Hacker News new | ask | show | jobs
by kazagistar 3017 days ago
Coming from functional languages, I really really tried to make optional work in our code base. However, it's inability to interact with checked exceptions or even slightly unusual control flow make it a real pain. It feels like fighting the language. My current suggestion is to pick a nullability annotation, and then wire it through your compiler and IDE, so it tells you if you forgot null checks, or made a superfluous null check on something annotated non-null.
1 comments

I was surprised by the responses about Optionals. Will have to look into it. As an alternative I have been looking to use the following to guarantee that certain methods cannot/will not return null (below). Maybe that is the better way to go?

return Optional.ofNullable(applications).orElseThrow(NullPointerException::new);

return Optional.ofNullable(applications).orElse(new Application());