3 ms·
It's not a get out of jail free card no, but it's value isn't in preventing null pointers. The value of Optional is in providing intent. You also get map, filt
by cetra3 11y ago
It's not a get out of jail free card no, but it's value isn't in preventing null pointers. The value of Optional is in providing intent.
You also get map, filter, flatMap, orElse, orElseGet, ifPresent. These are all great ways of making your code more functional, and do more with less.
I don't think annotations are the answer either, as you can't always rely on static analysis to prevent errors, a lot will still appear at runtime.
- Retra 11y agoIt does provide some type-level information, but if you're already writing documentation, the benefits are marginal. Add to that the fact that it really pollutes your interfaces, I would argue that it is almost always better to just pass a null and force people to check it, or read the documented guarantees. Basically, just assume every Java type is wrapped in Optional implicitly. But by adding Optional (and using it in standard libraries) it's often a matter dealing with code that interfaces between the two styles -- another problem that didn't exist without Optional. And even if you think Optional is worth the small gains, it's still a sad mimic of the Option types implemented in other languages.