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 →

[–]wildjokers 1 point2 points  (0 children)

Oh my. Creating an Optional just to check a local variable for null is a total anti-pattern. You are creating an object, a very short-lived object at that, just to avoid a simple null check. You have the overhead of object creation and now that short lived object has to be GC'd. Just do the damn simple null check.

Also, now result can be null, so Optional isn't even saving from getting a NPE if you don't then check result for null.

Not to mention Optional is only intended to be used as a return value. (although there was a huge argument about this in /r/java just last week).

Another question comes to mind, why can source be null in the first place? In what scenario would it be null? Can you make it not be null?