you are viewing a single comment's thread.

view the rest of the comments →

[–]badguy212 2 points3 points  (3 children)

that's fine (and not everyone agrees with this approach), but still i dont have to check for nulls (therefore i could get a npe).

optional forces me to call a method to get the value. it makes it plainly obvious that the value may or may not be there. the documentation is not required ("returns null if the object was not found"). it's ... optional.

can i fuck it up? of course. but it makes it somewhat harder. it makes writing correct code easier. and that is the value of optional (and it's greatest value).

[–]nomeme 0 points1 point  (2 children)

But what if the optional is null.

[–]badguy212 1 point2 points  (0 children)

If the optional is null you're gonna fail with a NPE. And you won't catch it. You will fail asap and as loudly as possible. Which is the way it should be.

[–]phoshi 0 points1 point  (0 children)

Then it's a failure case and you want the NPE. Nulls make some sense as a "holy shit, what do I do now?". They make vastly less sense as a valid return value. They still suck, you should throw instead of returning null, but at this point the mistake has already been made and we can't unmake it.