cxxr / better-java

Resources for writing modern Java
Other
5.79k stars 729 forks source link

Avoid passing null optional #11

Closed winterbe closed 7 years ago

winterbe commented 9 years ago

First kudos to your guide to better java. I like most parts of it.

In section Avoid Null I'd propose to not pass Optional parameters around because you cannot safely assume that those Optional objects aren't null itself. Instead I'd pass Bar directly and wrap itself as a nullable optional inside the constructor. That way you would prevent one possible NullPointerException during runtime.

pwittchen commented 9 years ago

Nice suggestion. :+1:

cxxr commented 9 years ago

I like this idea. However, I notice that I end up passing Optionals around sometimes just so I don't need to unwrap them. Maybe the fix here is to have both styles and comment on each?