Comments on: Returning ‘null’ Considered Dishonest https://andypalmer.com/2008/08/returning-null-considered-dishonest/ Views on software, technology, consulting and business process Fri, 02 Oct 2015 08:37:12 +0000 hourly 1 https://wordpress.org/?v=6.4.3 By: Maarten https://andypalmer.com/2008/08/returning-null-considered-dishonest/comment-page-1/#comment-77331 Fri, 02 Oct 2015 08:37:12 +0000 http://www.andypalmer.com/blog/?p=9#comment-77331 Andy, I wholeheartedly agree, and have been agreeing with you for many years, but only recently wrote an article about the common misconception of “exceptional circumstances” that dragonlips refers to and the fact that they don’t exist (and thus exceptions should be used).

And not only that, but also the fact that we should actually be using checked exceptions for everything, but there’s a big (but easily fixed) problem with the implementation of them in Java (and any other language for that matter in regards to regular unchecked exceptions). I also include a proposal for a solution by making a small language adjustment.
In a nutshell, checked exceptions should automatically be converted to unchecked (runtime) exceptions when they are not caught or rethrown. That way you almost exclusively deal with (throw and catch) checked exceptions (which I call functional errors) originating directly from the called function. Unchecked exceptions (which I call internal errors) are only to be caught at specific places in code (perhaps only the main() function) to log them and display a “Something went wrong, check the logs” message to the user.

Example: when calling login() I would like to catch UserNotFoundError and PasswordIncorrectError to display a useful error message on the login screen that the user should check the username and password and try again. I don’t care about any DatabaseNotFoundError, IoError, ConnectionLostError, etc. etc. because I can’t handle such errors originating from deeper functions called by the login function. Those are internal implementation details and need to be dealt with at another level in the application.

This is the link to the article:
http://blog.bitethecode.com/post/130259363012/exceptions-dont-excist

The pun in the title is intended.

]]>
By: Andy Palmer https://andypalmer.com/2008/08/returning-null-considered-dishonest/comment-page-1/#comment-1132 Fri, 16 Mar 2012 19:19:00 +0000 http://www.andypalmer.com/blog/?p=9#comment-1132 In reply to dragonlips.

I disagree (strongly) with the idea of exceptions being exceptional. They’re actually a half-arsed implementation of Lisp conditions (see Greenspun’s tenth rule)

I use exceptions when there is no point in the current logic path continuing. In the VendingMachine example, my friend Liz was able to write a Swing GUI that caught the exception and displayed it as an error message on the LCD display. Quite a result!To do that with return flags, I’d have to check the return flag, and then probably return a return flag to my caller, who’d have to return a return flag to his caller… and so on and so on.

Also, out parameters, by their very nature require mutable state… Not for me, thanks.

]]>
By: dragonlips https://andypalmer.com/2008/08/returning-null-considered-dishonest/comment-page-1/#comment-1131 Fri, 16 Mar 2012 18:19:00 +0000 http://www.andypalmer.com/blog/?p=9#comment-1131 programming by exception for “normal business logic” is a bad idea.

This is a prime example of where a result flag + out params is appropriate.

bool GiveMeaDrink(out Drink)

So now you know if it’s safe or not off one call. If you need to know why give some way to get that info. But really you’re looking into the state of the vending machine at that point and it doesn’t really need to tell you since you already know you were supposed to provide the adequate amount of money first.

You could argue that it’s better to check state first then call. However that’s only partially successful since it would fail in a threaded environment.

however the point is Exceptions are “exceptional” (hense the name) not “normal” you didn’t give me enough money so you can’t have a drink is not exceptional in anyway. You just need to learn how to count your change better.

]]>
By: Returning Nulls is Dishonest | Ramblings of a wanna be developer https://andypalmer.com/2008/08/returning-null-considered-dishonest/comment-page-1/#comment-1089 Thu, 03 Feb 2011 01:31:16 +0000 http://www.andypalmer.com/blog/?p=9#comment-1089 […] The post is here: http://andypalmer.com/2008/08/returning-null-considered-dishonest/ […]

]]>
By: Andy Palmer » Installing Eclipse https://andypalmer.com/2008/08/returning-null-considered-dishonest/comment-page-1/#comment-127 Sun, 22 Feb 2009 22:26:25 +0000 http://www.andypalmer.com/blog/?p=9#comment-127 […] empty (apart from the comment) method for void types, or return null;. I’ve already discussed my thoughts on returning null, and I would rather my code failed if it hits an unimplemented method rather than continue in a […]

]]>
By: Jamal Mavadat https://andypalmer.com/2008/08/returning-null-considered-dishonest/comment-page-1/#comment-23 Mon, 08 Sep 2008 14:35:00 +0000 http://www.andypalmer.com/blog/?p=9#comment-23 Try-get pattern is supposed to add a hybrid support!!! A solution to another problem…

]]>
By: Fredrik https://andypalmer.com/2008/08/returning-null-considered-dishonest/comment-page-1/#comment-22 Mon, 08 Sep 2008 08:04:00 +0000 http://www.andypalmer.com/blog/?p=9#comment-22 Calling Drink() when the vending machine has not yet received enough money would be an invalid operation for the current state of the vending machine – so f.ex throwing an InvalidOperationException would be OK here, imo.

If you don’t want an exception, you could use TryGet pattern:

Drink drink;
if(vendingMachine.TryGetDrink(out drink))
{
// we got a drink!
}

]]>
By: Jamal Mavadat https://andypalmer.com/2008/08/returning-null-considered-dishonest/comment-page-1/#comment-20 Fri, 15 Aug 2008 12:51:00 +0000 http://www.andypalmer.com/blog/?p=9#comment-20 @Andy, you’re most welcome matey, my pleasure for being here 🙂

Andy said: “I want my code to fail fast. If I don’t get a Drink, I want my code to tell me why.” As said before, both approaches can co-exist, and now you described a situation in which you shouldn’t be using null-refs! Perhaps solutions such as response-types (normal flow with additional info), or exceptions (exception or error flow optionally with yet more info).

Examples for returning nulls (sure we have alternatives):
1- getDefaultDrinkOrNull( ) if no-default-drink is considered a typical response and we prefer handling it in normal flow, fortunately designer has cleared all potential confusion by proper naming.
2- in a linked-list I may like getNext( ) return next object or null for the trail gifting more simplicity for iterations.

I think the “drink” example is a bit too generic! We could better be discussing null issues by real-world scenarios. For example:
A- If the Drink type is actually a Customer in a web scenario then returning response-types or exceptions are probably better approaches.
B- In a getTempFile( ) : File I might go for exceptions if it cannot return a temp file!
C- And for getActiveUser in a security service I might prefer returning null for indicating no-active-user message – however, I myself have designed security services in which active users were returned by response-types and some specific exceptions too. My mind wasn’t changed, they were just proper solutions for their unique requirements.

All I try to say is, we should NOT replace null-handling, BUT we may discuss a particular case in which null-handling is not the best.

]]>
By: Andy https://andypalmer.com/2008/08/returning-null-considered-dishonest/comment-page-1/#comment-19 Thu, 14 Aug 2008 20:00:00 +0000 http://www.andypalmer.com/blog/?p=9#comment-19 @Jamal, thanks for commenting.
I want my code to fail fast. If I don’t get a Drink, I want my code to tell me why.
I don’t want my client code, that is expecting to receive a Drink, to have to check that I didn’t cheat him and give him a null instead 🙂
Can you give me an example of when a null is a valid response to something that is expecting a Drink object? That is, can you describe a situation where an object that needs a Drink to work with, would be happy with a null?

]]>
By: Jamal Mavadat https://andypalmer.com/2008/08/returning-null-considered-dishonest/comment-page-1/#comment-18 Thu, 14 Aug 2008 19:22:00 +0000 http://www.andypalmer.com/blog/?p=9#comment-18 @Andy
IMO classic programmers are usually implementing C-Style control flows! Programming languages with exception support however add more convenience and support to the case, BUT do not replace null-reference handling. Here we go (I know this isn’t the perfect example):
if(( machine = getActiveMachine()) == null) { … }
else if(( drink = getMeADrink() == null) { … } …
In the above example we don’t expect something “exceptional” and everything is checked sequentially branching should a particular condition triggers – this is supposed to be the normal flow.

BUT exceptions are beautiful if it’s really considered an “exception” and should pick an alternate flow instead of the regular normal flow. For instance, designer of openFile( ) method MIGHT decide to throw an exception if invalid filename supplied!

Good design employs a mixture of both types; the point is to properly decide what should be considered the “normal” flow and what should be the “exception” or “error” flow! The rest is THE ART OF DESIGN… in summary I stand for proper use of null-handling and exception-flows, not eliminating any of them…

]]>