DEV Community

Discussion on: Code Smell 12 - Null

Collapse
 
jayjeckel profile image
Jay Jeckel

The ridiculous cargo cult hate that null has developed in recent years is one of my pet peeves, but I'll try to avoid a full blown rant and stick to one incorrect premise of this article: "Null does not exist [in the] real world."

Null absolutely exists in the real world. You know what I'm holding in my hand right now? Nothing, ie null. I'm not holding an object that represents nothing, I'm holding nothing, no thing, not anything. There is no better code representation of that state than null. What smells is this modern demand that we all pretend an object can represent no object.

You know what doesn't exist in real life? Anything resembling a NullCoupon. Besides that, you are now instancing yet one more object that has to be tracked and garbage collected, all because of some irrational dislike of null. So in addition to being a less accurate representation of the real world, this pattern is also less efficient.

Tony Hoare is wrong, he did not make a mistake by including null, because null is a valid state, both in programming and the real world, and coders making mistakes like not checking for null isn't a problem with null any more than not checking for 0 before division is a problem with 0 itself.

Collapse
 
mcsee profile image
Maxi Contieri
  • you hold "nothing"
  • a baby has 0 years
  • The space has vaccuum
  • My wallet has no money

Different things with different behaviors.

I keep thinking Tony Hoare is right :)

I will stick to this Besides that, you are now instancing yet one more object that has to be tracked and garbage collected. and the answer is 'who cares about GCs?' We are talking about good models, no premature optimization leading to early and tight coupling.