Null Object: Something for Nothing (2003)
researchgate.net
researchgate.net
*Well, ok, not /that/ surprised, given the year of publication, but Objective-C did at least exist at the time.
For C# programmers I attempted to help fix the problem[1] (it papers over the cracks):
I write much more Obj-C than Java, and I'm always kind of surprised going back to Java that null is such a big boogaboo all over the place. Considering how much influence Obj-C had on Java, it's a shame Sun didn't adopt "benevolent nil".
If you wrote "a = b/c" it would be silly to complain that this operation 'does nothing' whenever b == c happens to be true, so by analogy, it's not too farfetched to think it's useless to write code that does nothing when given a null object.
On the other hand, multiplication and division are standardized and familiar operations. Object semantics differ for each kind of object, and it may not make sense to allow null objects for everything, and if you need one, you can always implement it later.
So it really depends on what you think the tenets of OOP are, and that's not a cut-and-dry question.
Of course, it's too late for Java now. They have an Optional<T> type, but removing null from the language would break backwards compatibility. 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 :).
http://types.cs.washington.edu/checker-framework/current/che...
An Option type (and/or an Either type as well) is a good base, but if you can create a null object itself it's still better.
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.
Lombok can help with this, with its @NonNull annotation, but that only helps so much.