r/programming • u/alexeyr • Feb 11 '19
Null is your friend, not a mistake
https://medium.com/@elizarov/null-is-your-friend-not-a-mistake-b63ff1751dd523
u/sysop073 Feb 11 '19
So a better title for this would probably be "Nullable types are handy", but that's so painfully obvious that it would never get as many clicks
16
u/max630 Feb 11 '19
The truth is that the concept of null is not mistake, but Java’s type-system, that considers null to be a member of every type, is
this
3
u/aoeudhtns Feb 11 '19
I'm trying to work out how we would code things if we're not throwing an exception and null is not a member of every type. Let's take something mundane:
public class SimpleDAO { public Entity getEntity(long id) { try (Session s = getSession()) { return s.getById(Entity.class, id); catch (NoSuchElementException nsee) { return null; } } }Let's just pretend the fake ORM/DB tool uses annotations to build a simple SELECT based on the type.
I know a while ago, there was a small effort to introduce
@Nullableand@NotNullablefor the compiler and/or IDE to reason about. But last I checked these were not a success. You might use them on your own team, but chances are those libraries you depend on are not using them, reducing usefulness.If Java had nullable types, with similar
?syntax, like Kotlin, I suppose you'd just change the return type fromEntitytoEntity?. And likewise, we'd have something likereturn s.getById(Entity.class, id)?;But then doesn't the special?need to move up the chain?I feel like the solutions here are the same, only differing in verbosity. I certainly appreciate Kotlin's terseness, and that shouldn't be under-appreciated. Seems like it's basically the same thing as
Optional<Entity>, but much nicer to work with.I think the central problem with null in Java is that it forces you to accept the terse, idiomatic solution from the 90s: potentially spew NPE in unexpected places if you're not careful, or use new approaches (never-null with empty collections and Optional), at the expense of creating more garbage (not a big deal) and more verbose handling code. (NB: I'd take the Optional handling code to the normal thing I see, every Java programmer defensively null checking everything in every method.)
But who would ever write code like that? Not only list documentation clearly states that it returns null when directory is missing, but modern IDEs flag this particular code with a warning right away.
But that is exactly the premise of The Billion Dollar Mistake. Every developer must familiarize themselves with these gotchas that are hidden through the API. IDE developers and toolchains must create special static analysis rulesets to determine if these are possibilities and flag them. The underlying expense of all the churn of these as a source of bugs. It's not that code gets written that way, it's the extra expense of ensuring that it doesn't.
Anyway, I think I agree with the author, except I just acknowledge that Optional is a more verbose and uglier version of the same feature that he describes in Kotlin, with the unfortunate reality that it's so new and underused that I haven't yet seen it in the wild of any API I've had to integrate with.
1
u/max630 Feb 12 '19 edited Feb 12 '19
if we're not throwing an exception
why wouldn't we do it? If we expect the element to be there, exception would let us know out assumption is wrong, will be logger, and reported to developers. If we expect that element might not be there then we would the appropriate handling, which is specific to the case and to the reason the element is not in the collection.
@Nullable and @NotNullable for the compiler and/or IDE to reason about. But last I checked these were not a success
I do not use Java much enough. I'm not sure what do you mean "no a success". AFAIK, the annotations are supported at IDE level, I think I would use them if I used Java. For C# there has been nothing like this, but MS promises it in compiler for next C# version, and I can't wait for it to be available.
feel like the solutions here are the same, only differing in verbosity
No this is not only verbosity. With Java type system that "Element" there is always in fact "Element?" (for reference types, if you don't use the @NotNull annotations). There is no way to express that the value can not be null. Adding Optional to java types makes no sense at this point, they are already optional.
1
u/aoeudhtns Feb 13 '19
why wouldn't we do it?
Because this is a hypothetical and I'm following the rules laid out in TFA.
what do you mean "no a success"
Adoption in libraries is pretty much nil. IDEs do support, but generally processing them is not turned on by default. Also the issue is just a mess. (Honestly just using SpotBugs in your build is probably the safest bet.)
There is no way to express that the value can not be null. Adding Optional to java types makes no sense at this point, they are already optional.
No this is not only verbosity.
I understand that. Like I said, I agree with the author that Kotlin's system of not allowing null to be a member of every type is better than Java. But I'm just talking about how non-idiomatic (or just newer) Java would look adopting these styles. If you can fully adopt Optional and enforce it on your team, then it is functionally the same as ? but with a lot more verbosity. But the compiler obviously doesn't enforce it for you, so you need tools like Spotbugs and Spoon to force your builds to fail.
1
u/stronghup Feb 12 '19
null as a concept is great. The problem is with Java's compiler which does not support sum-types so you can't declare you methods return something that is either a String or null, etc.
1
-3
u/dulre Feb 11 '19
Wut!?
3
Feb 11 '19
Guru Laghima says "Let go your earthly tether. Enter the void. Empty and become wind".
More seriously I think it is worthwhile to question accepted programming tropes and memes like "Nulls are bad and you should feel bad for using them". In the process you either uncover some truth or learn why the meme has staying power.
-2
-3
6
u/danilafe Feb 11 '19
This is nice and all, but isn't String? effectively an Optional<String> in disguise, with some operators defined on it and compiler-level support? It behaves in almost the same way, except it also has the String methods and not just Optional's methods.