JSR-305, checker framework, Optional are all half-baked workarounds evidenced by the lack of their adoption.
JSR-305, checker framework, Optional are all half-baked workarounds evidenced by the lack of their adoption.
I can’t remember the last time I encountered one by using the proper compile time checks. It does need to be enforced organization-wide, and not partially with annotations, but if you can make that change then you can code in Java without the mental overhead of null.
Or simply wrap the return with Optional.ofNullable, checkstyle will not accept it if you don’t.
When you're sufficiently careful, you can reduce accidental nulls down to the level of minor inconvenience.
But no amount of care on your part will stop your teammates from deliberately using nulls.
Even IntelliJ right now will tell you off for using an Optional field instead of a nullable one.
> then you can pretty much eliminate NPE.
NPEs is only one kind of cost incurred by the lack of null safety. The other is all the unnecessary "if (x == null) {" boilerplate code caused by the uncertainty and defensive programming, which increases complexity and worsens readability.
It's not. It's another half-feature into the collection of half-features mentioned in my post.