DEV Community

Discussion on: Unhealthy Code: Null Checks Everywhere!

 
olvnikon profile image
Vladimir

I think the whole idea of using NullObject is taken from Option type, which holds None or Some. When they is no null you don't think it exists. E.g. while Some holds a real not null value, which can be mapped, None always returns None. This approach is called "Railway programming".

fromNullable(null).map(double).map(tripple); // None
fromNullable(2).map(double).map(tripple); // Some(12)

null is avoided and the program doesn't crash. Just different way of thinking when there is no null.

Thread Thread
 
dimpiax profile image
Dmytro Pylypenko

As I know, the pattern was a long time before Optionals (some, none).
And what author proposes – it’s a big difference from it.
Using optionals you stop the execution flow, while the topic about redundant objects creation.

Thread Thread
 
lukejs profile image
Luke

2 examples I've seen in Minecraft:

Air blocks - they are blocks like any other, rather than nulls to represent a block not being present in a location

Also added in the last few versions (replacing null) - "Empty" item stacks - there is a singleton "ItemStack.EMPTY" and a method "ItemStack#isEmpty()"