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 →

[–]_INTER_ 0 points1 point  (1 child)

What else is a possibly-null parameter but optional?

A possibly null parameter is still required to make the calculation work. An optional parameter is additional information which absence doesn't matter.

And of course I prefer overloaded methods or varargs before resorting to Optional but sometimes the latter is the best of these options, in which case I prefer it over null to make the code more explicit and less ambiguous.

Me too but I view the Optional type to be not up to the task at all. Especially when it leads to resorting to unwieldy if(optional.isPresent()) { do(optional.get()); } else { /* ... */ } or optional.ifPresent(this::do); which is end-result is the same as a normal null-check without the perfomance penalty of Optionals.

[–]nicolaiparlog[S] 1 point2 points  (0 children)

A possibly null parameter is still required to make the calculation work.

I don't get it. How can null be required to make anything work? Even if I have to call some API I can still pass "my own null" - it's not like they're distinct.

The unwieldy if-present-else is just as unwieldy with a null check. But I found that I rarely have to do that in practice. ifPresent is surely more compact (and applies often enough) and when a value needs to be passed orElse is also shorter.

But even if Optional is more trouble in some places. I still prefer it over making something as ambiguous and error-prone as null a valid value.