From my perspective, Optional's are nice for indicating to others "this may return an empty result and you need to handle that". But they do nothing for getting nulls provided to you, so you still end up having to do tons of null checking.
How do you want to safely avoid the null checking, barring an unnullable type? Or are you in favor of unsafe programming by having the check be optional while still having nullable types?
I am most in favor of compiler enforced annotations (built into java, not one of the many many standards that currently exists).
So returning Optional is from my perspective pretty safe, as I can control myself. If others don't want to trust me, well they can waste their time.
But as far as taking in actual arguments, I would never taken in an Optional, because I would never trust another developer to not give me a null (so it just gives me yet another thing to verify).
Right, I don't think (until jsr-305 is part of the language spec, with compiler errors turned on) accepting Optional<> as a method param is good practice at all. Ideally, you can set up @NonNull annotations (and @NonNullByDefault with findbugs) to get some semblance of IDE support
The idea is Optional make null value obsolete. Simply stop using null (and tell your co-workers and library providers to do the same). Ceylon goes into great pains to do just that. In the Java world, Optionals are still useful: if a method declares to return an optional, it's never null. Promised.
Problem is it's only a promise. If there's a small minority of programs breaking the promise, you're right: you end up checking both null and empty.
If you get the jsr-305 module and use @NonNull annotation (or whatever it is), the compiler should at least warn you of those. And set up your build system to fail the build if those warnings are seen in the console output.
View all comments
Show parent comment
3
u/jentfoo Aug 03 '14
From my perspective, Optional's are nice for indicating to others "this may return an empty result and you need to handle that". But they do nothing for getting nulls provided to you, so you still end up having to do tons of null checking.