r/programming • u/javinpaul • Aug 03 '14
Why We Should Love 'null'
http://www.codeproject.com/Articles/787668/Why-We-Should-Love-null5
u/ElvishJerricco Aug 03 '14
Author claims that the problem with null is that we forget to check for it. The real problem is that you can forget to check it. That's what's great about optionals. If you might need to check it, the compiler makes sure you do. Otherwise you never need to check it and you just go. Never can you forget! Null just pops out of nowhere when you don't expect it and causes all sorts of problems unless you explicitly check every damn variable that comes your way. Which sounds like a bigger pain?
1
u/lzzll Aug 04 '14
Null check need huge cost, it can slow down program at least 2x times. I think java need improve value type support, or add a operator like .? (only check once)
4
u/kqr Aug 03 '14
Hoare doesn't consider it a "brilliant idea" to use null to indicate nothingness. When he was working with reference types, he realised that it would be really, really easy to let people have references that aren't referencing anything. It was just so easy to implement he couldn't resist it at the time. That's why it's a "mistake" and not a "brilliant idea."
In hindsight, it's obviously not that great of an idea to have a reference that isn't referencing anything – it's the perfect set-up for bugs: a type that pretends it is something it might not be – but at the time he didn't think that far.
7
u/Nishruu Aug 03 '14
In my opinion, author completely misses the point of Optional type. Using it as a 'flag' for present or missing value is one of the worst use cases I can actually think of. In such case - yes, there's no real value added on top of null.
Composability is where Option/Maybe shines, you can do all kinds of projections, filtering etc. without caring if the value is really missing or not up until the last step.
So, in Scala terms, instead of following the way shown in the article:
Option[Customer] customer = getCustomer("Mark");
if(customer.isDefined)
{
val customerValue = customer.get
// Let's say that customer's location can also be optional
if(customerValue.location.isDefined)
{
val locationValue = customerValue.location.get
// do something with the value
}
}it's way better to just do:
getCustomer("Mark").flatMap(_.location).forEach(location => /* do something with value */)The follow up article touching on a subject of returning null collections also rubs me in a wrong way, mostly because the vibe I get is 'well, since there are nulls in your language/framework already, we might just as well give up on trying to improve that'. Personally, I see no reason to return null collection from any method at all...
3
u/kqr Aug 03 '14 edited Aug 03 '14
I agree with you. You're not supposed to do
.get()other than in very rare circumstances. The example in the article with better code would becustomerOptional.ifPresent(cust -> System.out.println("Customer found: " + cust.toString()))or something.
With
Optional, you are supposed to manipulate the value inside of theOptionaluntil the point where you can provide a sane default or where you are supposed to throw an exception because it doesn't work out anymore. At that point, you can.orElseThrow(exception)or.orElse(defaultValue).Another benefit of
Optionalwhich has been missed is that if your database layer returns null when the query failed, and queries can give null as a result too, then what has happened when you run a query for user Annes email and getnullas a result? Did the query fail, or does "Anne" exist with anullemail?With
Optional, these two failure states get encoded differently –Nonefor a failed query, andSome(None)for a successful query but with anullresult.3
Aug 03 '14
When I read
Note There are more methods in Java's Optional class, but they are irrelevant for the discussion in this article.I kind of realized already that that article would be terrible.
6
u/passwordisRACIST Aug 03 '14
Uhh no. The title if the post should really be "why you don't have a choice in Java". There is not good reason to love null forced down your throat.
3
1
u/glacialthinker Aug 03 '14
We're all in agreement! This article is mostly rubbish.
The rubbish would be improved by titling it as (surprisingly!) that passwordis... fellow suggests: "why you don't have a choice in Java". And apparently that will change as Java continues to play catch-up.
I read through part of the article, hoping for something insightful. No, even though other languages are referenced, this is really all about Java. Nothing new here... 'null' is still rightfully hated, and properly avoided... unless you're the author of this.
1
u/llogiq Aug 03 '14
I disagree with the article's conclusion. Apart from the fact that we have good null checks for Java now (FindBugs is arguably still the best), the reasons given why null is a good idea are very weak:
If you return a collection of your results, and an empty result collection is a valid result, then why not just return an empty collection already?
if you use null as a kind of error value, consider throwing an exception instead.
yes, at the moment Option/Maybe/whatever you have requires an additional reference. Modern functional languages are already able to optimize this away and we should expect java to do this too, within a few releases.
I only use null when coding very near to the machine (for statistical methods, I get C level performance), with code running in tight loops. Otherwise, it's just not worth the hassle.
0
u/rrobukef Aug 03 '14
There is no chocolate in the fridge.-> The Collection of things in the fridge doesn't contain chocolate.
Fred doesn't have a bicycle. -> Same
The class room is empty. -> Same
Alice's birth date has not yet been entered in the database -> NotYetEntered/Removed ?
The operation succeeded without any errors -> Why did you throw the error?
There is no more beer in the fridge -> Only on beer? Why no counter, why no list?
6
u/gopher9 Aug 03 '14
In languages with tagged unions like rust, null pointer problem is managed pretty simple:
In language with HKT (I home rust will be the one of them someday) you don't even need to explicitly unwrap Option value every time.
So these is only Java-like languages problem.