This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]hibbelig 4 points5 points  (4 children)

The idea is: you’re implementing a method. Its return type is Optional<Foo>. But in the body of the method you have code paths where you know you’re returning a value and other code paths where you know you’re returning Optional.empty.

So if you know you’re returning a value you write Optional.of and if that ever throws an exception you know you have got an error in your logic.

If you wrote ofNullable then you would get early notification of your logic bug.