Hacker News new | ask | show | jobs
by stickfigure 1863 days ago
Java went the route of Optional<?> instead of nullable types. You still end up with some nullability issues working with old libraries (and even the std lib, like Map.get()) but by and large you can force a "never use null" policy on Java codebases and NPEs become very rare.

I fairly strongly prefer Optional over nullable types. Especially how it works seamlessly with streams.

2 comments

It's actually kind of instructive to consider how Scala mostly solved the NPE issue by simply having Option available and discouraging use of null.

There are still issues at the API boundaries with Java or JS code, but that's about it. Outside of that, NPEs are usually in more niche places to do with initialization order and that sort of stuff.

Of course if you're inventing a totally new language you probably want to just avoid the possibility of null outright.

The "never use null" policy doesn't work across your third party dependencies, unless those dependencies coincidentally use the same policy as you.