Eliminates null checks, Optionals, and NPEs. Probably moots annotations like @NotNull too, but maybe some use cases need those for client APIs.
Makes iterating data structures like graphs simple. Faster too, because the JIT NOP a Null Object's methods. (Mostly; profile to confirm, then tweak as needed.)
I implement a Null Object for each base class.
Each base class has a static final member NULL referencing its Null Object implementation (flyweight, singleton). Then assign variables to AwesomeThing.NULL instead of null.
A spiffy javac could code generate Null Object implementations. (It's on my todo list.) For scalars, just use the default null value should be. int is 0, float is NaN, etc. Their boxed values will need small shims too.
Customizing javac (with some compiler plugin or something) is deep down on my TODO list. So I'm unlikely to be the (first) person to do this work. Sorry.
Objective-C does that but it has the effect of separating serious consequences from their causes. Places that really require non-null have a much more difficult time finding where the null value originated.
And since Objective-C contained a lot of C, it really just separated null dereferences from their causes making debugging much harder.
Thanks for replying. I really appreciate pushback, criticisms, and thoughtful opinions.
I don't know Obj-C, so I'll have to research those use cases, to understand better. eg: Are those across processes? Is this an Obj-C idiom? How to make illegal state representations impossible?
Per the Null Object pattern description, Null Object captures all of the null handling and behavior in one place, vs scattered around. Stuff like null checks and throwing exceptions. It does not eliminate the need for the concept of null.
I'm probably (mostly) wrong about eliminating @NotNull annotations. Especially in method signatures (defensive programming). Last night while commenting, I was thinking about annotating POJOs, to info serialization.
What I mean is that semantically there's often no such thing as a null object for a given domain. So any artificial one that can be made is indistinguishable from null in practice (i.e. all operations on it will throw etc).
Eliminates null checks, Optionals, and NPEs. Probably moots annotations like @NotNull too, but maybe some use cases need those for client APIs.
Makes iterating data structures like graphs simple. Faster too, because the JIT NOP a Null Object's methods. (Mostly; profile to confirm, then tweak as needed.)
I implement a Null Object for each base class.
Each base class has a static final member NULL referencing its Null Object implementation (flyweight, singleton). Then assign variables to AwesomeThing.NULL instead of null.
A spiffy javac could code generate Null Object implementations. (It's on my todo list.) For scalars, just use the default null value should be. int is 0, float is NaN, etc. Their boxed values will need small shims too.
Customizing javac (with some compiler plugin or something) is deep down on my TODO list. So I'm unlikely to be the (first) person to do this work. Sorry.