tbitonti / jakartaee-prototype

Other
0 stars 9 forks source link

Reconsider using `Optional` instead of using `null` #56

Open bjhargrave opened 4 years ago

bjhargrave commented 4 years ago

Returning an empty Optional is no more expensive than return null.

Using Optional can avoid numerous null checks at call sites (which can be forgotten as new code is written).

Modern JVMs are very efficient at managing short-lived objects, so using an Optional to wrap a result is not excessively costly. Of course, actual performance data to prove using null is better here can change the story, but until we have such concrete performance data, we are better off using better programming practices and not using null for control flow.

tbitonti commented 4 years ago

See: http://blog.codefx.org/techniques/intention-revealing-code-java-8-optional/