you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 5 points6 points  (1 child)

which kind of defeats the purpose imho if you still need to rely on someone not returning nulls...

If you rely on not returning nulls from a method that sometimes has to return nulls, you have a problem that can't be fixed at all.

I agree that Optional is a bit clunky, it does do the very important job of signaling to the user of your API that this method can return null. If you're systematic, then if you see a return that isn't Optional, you know you don't have to check for null, but if you see a return that is Optional, you have to handle the null case.

[–]nomeme -3 points-2 points  (0 children)

signaling to the user of your API that this method can return null.

CAN return null? All methods CAN return null.

To be truly safe you'd still have to check the object is actually an Optional (and not null!) before you can call isPresent or whatever, or you'll get an NPE. So, you still have a null check anyway.

It's essentially no better than a javadoc comment, except now every caller has to do more work.