{"id":9,"date":"2008-08-10T19:00:00","date_gmt":"2008-08-10T19:00:00","guid":{"rendered":"http:\/\/www.andypalmer.com\/blog\/?p=9"},"modified":"2009-05-22T13:09:32","modified_gmt":"2009-05-22T13:09:32","slug":"returning-null-considered-dishonest","status":"publish","type":"post","link":"https:\/\/andypalmer.com\/2008\/08\/returning-null-considered-dishonest\/","title":{"rendered":"Returning ‘null’ Considered Dishonest"},"content":{"rendered":"

Background<\/h3>\n

Antony Marcano<\/a> and I have just started running a coding design workshop. Most of the audience are new to coding and we are trying to focus on good habits that are applicable across all programming languages.
In our first session, we created a vending machine. By the end of 90 minutes, it was able to dispense a drink as long as it had sufficient money and stock available.
One of the questions that we asked was “What do we do when the customer has not inserted enough money and we press the button for the drink?”
Some of the people who had some programming background said “Return null”, which is what brings us to this post.<\/p>\n

Good Citizen<\/h3>\n

In the wiki for the PicoContainer project, there is a page titled “Good Citizen<\/a>“, which details a number of good practices that all Java classes would benefit from trying to adhere to.
The practices make a lot of sense when you think about them, but they aren’t really explained. I’m going to try and address that issue as we cover them in the workshop.
The practice that we are looking at today is “Never expect or return null”<\/i>, with a dash of “Fail Fast”<\/i> for flavour.<\/p>\n

What’s so bad about null?<\/h3>\n

In the Java world, when we declare a method signature, we specify a return type. In the case of our vending machine we had:

public Drink giveMeADrink() {...}
<\/code>
By declaring that the return type is Drink<\/code>, we are signing up to return something that “is-a”<\/i> drink.
We could<\/b><\/i> also return null<\/code>, which is a nothing value. We could<\/b><\/i> use this to represent that you did not get a drink.
The client code may look something like this:

myVendingMachine.giveMeADrink().downInOne();
<\/code>
If we return null<\/code>, this code will fail with a NullPointerException<\/code>. Not particularly useful, but at least we are using the result straight away. The problems become much worse if we store the returned Drink<\/code> for use later.
When we said we will always return a Drink<\/code>, we lied<\/b>.<\/p>\n

Programming Defensively<\/h3>\n

The sample client code above makes the assumption that the result of giveMeADrink<\/code> will be a Drink<\/code>. Given that we’ve actually signed up to that contract, that doesn’t seem to be unreasonable. But now the client code is broken and they have an angry customer, they are going to have to work around the issue. It would probably look like this:

Drink myDrink = myVendingMachine.giveMeADrink();
if(myDrink != null) {
myDrink.downInOne();
}
<\/code>
This code is actually saying “I’ve asked you to give me a drink, but I don’t trust you<\/b>, so I will check first”.<\/p>\n

Why isn’t this working? An Exceptional Approach<\/h3>\n

If we rely on our client to check that they received a valid result, we lose out on an opportunity to let the client know why the call was unsuccessful.
In the Programming Defensively example, we can recover<\/i> from being returned a null<\/code>, but we don’t know why<\/i> it was null<\/code>. Was it because we hadn’t inserted the money? Was it because the machine was out of stock? Was it because the stars were out of alignment?
Do we handle the scenarios differently? If we haven’t inserted enough money, that’s something we can deal with, but if the machine is empty, we need to look for another machine.<\/p>\n

What if our code looked like this?

public Drink giveMeADrink() {
if(weDontHaveEnoughMoney()) {throw new NotEnoughMoneyException();}
if(weDontHaveEnoughStock()) {throw new DrinkOutOfStockException();}
return new Drink();
}
<\/code>
What we have said is “We will always give you a drink or tell you why we couldn’t<\/i>“<\/p>\n

Now when we attempt to call giveMeADrink<\/code>, it lets us know straight away if it can’t proceed. It also gives us a good indication of why<\/b> it is having problems.
The client code calls:

myVendingMachine.giveMeADrink().downInOne();
<\/code>
and gets told “Sorry, I’d give you a drink, but you didn’t insert enough money<\/b>“.
Our code is being honest, polite and giving the client an opportunity to remedy the situation. The customer is still angry, but now he’s angry with himself for not putting in enough money.<\/p>\n

<\/p>\n

In Summary<\/h3>\n

<\/p>\n

    <\/p>\n
  • Programming defensively is programming distrustfully<\/li>\n

    <\/p>\n

  • Returning null is dishonest. It requires others to check that we’ve upheld our side of the bargain<\/li>\n

    <\/p>\n

  • Throwing a meaningful exception allows us to let the caller know why<\/i> their call did not succeed<\/li>\n

    <\/ul>\n","protected":false},"excerpt":{"rendered":"

    Background Antony Marcano and I have just started running a coding design workshop. Most of the audience are new to coding and we are trying to focus on good habits that are applicable across all programming languages.In our first session, we created a vending machine. By the end of 90 minutes, it was able to […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[20,19,17,21],"_links":{"self":[{"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/posts\/9"}],"collection":[{"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/comments?post=9"}],"version-history":[{"count":2,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/posts\/9\/revisions"}],"predecessor-version":[{"id":281,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/posts\/9\/revisions\/281"}],"wp:attachment":[{"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/media?parent=9"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/categories?post=9"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/tags?post=9"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}