Hacker News new | ask | show | jobs
by virgilp 3116 days ago
The big problem with java is that you don't have non-nullable versions for most types; in my mind, the biggest advantage of Option is the default implication that "everything that's not explicitly Optional is really non-optional". Alas, that's not true in Java - if you have a non-optional type, you can (almost) never be sure that the value is non-null. It feels like just adding Optional, they only did half of the job.
3 comments

In this case the most pragmatic thing to do is to ensure nothing in your application can ever be null - use asserts during development, empty lists, null objects, etc. Also never catch NPE's, if there is one after all those precautions, it's a developer error / bug and should be punished as such.

The problem with optionals - as Sonar will complain about - is that people will call `.get()` on an Optional without first checking `.isPresent()` for example.

Lots of things in the language that allow you to shoot yourself in the foot that other languages have solved.

Semantically, is that different from using a reference variable without first checking it !=null?

Also, isn't there a static code analysis tool that checks at compile-time that reference variables annotated as @non_nullable can never be null?

There is exactly one place where you might want to catch an NPE: top level global exception handler for logging and debugging.
Absolutely. So, what I started to do in my java code: Before dereferncing anything for the first time, i add

    assert foo != null : "foo must not be null - remember to wrap optional data in Optional<T>"
Just make sure to run your program (or at least the tests) with the JVM flag "-ea"
What is the point? How is an assert better than a NullPointerException?

If asserts are disabled, then the null pointer checks are still inserted by the JVM.

The benefit of using an assert is that it makes the error condition known immediately to the caller and the reason. Otherwise, whose to say when the NPE will occur. It could be raised just a couple lines down, or it could be a couple methods or a classes away which increases the complexity of diagnosing the actual cause of the error.
A more idiomatic approach would be Objects.notNull(foo)
They cannot truly fix the problem without dropping backwards compatibility.
That's only partially true (and it is harder to do now that they introduced Optional).

Think about it like this: in Java - all class references were Option<Class> by default - and what was truly lacking was the possibility of having a non-optional reference[1]. They could've treated transparently all old-code[2] references as Option<ClassName>, and just given you the possibility of having non-optional reference in the new code (that meant that you can't pass objects with non-optional references to the legacy libraries, but it's a pain that could've gone away pretty quickly as libraries got recompiled to the new java versions)

[1] Oracle's interpretation was the other way around: "Java lacks the Option type, let's add it". I argue they got it wrong - Java always had the Option type, it's just that it was implicit; and since Java was lacking the non-optional types, you couldn't do anything useful with the Option, other than check for "None" (i.e. null).

[2] By "old code" I mean "old compiled code". You keep the binary compatibility - but indeed, you would need to drop the source-level compatibility, since compiling old code with the new compiler would not have worked. But, again, that would be fixable with a "legacy syntax" compiler flag, that would produce old-style binaries from old-style source code.

I'm not sure if it is going mainstream right away, but C# is trying an interesting solution to that same problem: reversing their default relationship with 'nulls' and adding in optional syntax to flag variables as 'nullable'.

In terms of backwards compatibility the language teams position, IIRC, was that in the cases where you are intentionally using null it's quite easy to find and flag flag the nullable variable, but in all other cases the unexpected nullability can responsibly be removed from apps, making then-unnecessary null checks superfluous.

It's a bold move, but if it works at scale Java might be able to pull the tablecloth out from under the plates too...

Could the add some non-nullable annotation like apple did when updating objective-c to play nicely with Swift's optionals?
Non-nullable is a promise not a guarantee. It does absolutely nothing in terms of useful warnings or compiler errors within Objective-C, it only explains how Swift should consume the Objective-C API.

It's still entirely possible to get a nil from Objective-C while the API is declared as nonnullable, this even happened to me with Apple's own API's if abused to a certain limit.

Yes but if Java 10 (for example) introduced this annotation then the JVM10 could transform a null returned from a non-nullable annotated function to an exception. Compile time static checks could also be added to guarantee that such a method does not return null. But you could still use such a function from a previous java version albeit you would sacrifice the checks.

It is a way to introduce the benefits (as long as the library is developed properly) of non-nullable types without sacrificing retro compatibility.

Edit: I remember a bug in earlier version of Swift Cocoa binding where getting calendar events without titles would hang without possibility of recourse because of this problems.

Something like what they are trying for C# may work for Java too: https://blogs.msdn.microsoft.com/dotnet/2017/11/15/nullable-...