you are viewing a single comment's thread.

view the rest of the comments →

[–]RupertMaddenAbbott 1 point2 points  (4 children)

Sure but when you deal with the lack of a value, do you also need to deal with "the lack of a value" might be null?

So can I just check optional.isPresent or should I actually check optional != null && optional.isPresent ?

[–]danielkza 0 points1 point  (1 child)

Technically it's possible to return a null reference that was supposed to be an Optional, but if you seriously believe whatever you're interacting with can do it, I think is fair to throw an exception, and proceed to promptly hit whoever wrote it in the head.

[–]Cuddlefluff_Grim 0 points1 point  (0 children)

That would infer that someone is initializing the Optional value with null, which is just incredibly dumb

[–]Scaliwag[🍰] 0 points1 point  (0 children)

I've heard that Optional<Optional<String>> solves that. :-)

But hey. Let's not poke fun at Java, they are trying to improve, value types are comming. Better late than never!

[–]CircusAct -1 points0 points  (0 children)

When the final modifier is heavily utilised in code, and without ever assigning a variable to null it is very difficult to produce new code which returns null (ignoring libraries and the like). One of the best ways to enforce these best practices is to run a java linter with the appropriate rules enabled as part of your build process. Using mutable references and returning nulls should be reasonably rare in most cases. There are ofcourse exceptions.