5 ms·
> Of course, that means that a method that returns Optional<T> might return an Optional<T>, or null, so now you have 2 things to check instead of one :). NO. J
by Dewie3 11y ago
> Of course, that means that a method that returns Optional<T> might return an Optional<T>, or null, so now you have 2 things to check instead of one :).
NO. Just don't check for null and let it fail -- that will uncover a bug, namely using `null` when they should have used "None" or whatever it's called in Java.
Do people defensively check for null for every object access in java? I hope not, because that would be really tedious and obfuscate all logic bugs relating to inappropriate uses of null.
- wtetzner 11y agoThe problem with not checking for null is that it won't fail soon enough. null will get passed along until someone tries to call a method on it, at which point you'll get a NullPointerException. But at that point you don't know where the null came from. It's better to fail early. Lombok can help with this, with its @NonNull annotation, but that only helps so much.
- Dewie3 11y agoDo you assert that all object variables that cross an interface boundary are not null (`assert var != null`)?