Null, undefined and empty values: why "nothing" causes so many bugs
Tony Hoare, who introduced null references in the 1960s, later called it his "billion-dollar mistake." The problem isn't that "no value" exists; it's that many languages let any reference secretly be null, so the type system can't warn you.
The many flavors of nothing:
null/nil/None: an intentional absence of valueundefined(JavaScript): a value that was never set- Empty string, empty list, zero: real values that are often treated as "nothing" by mistake
NaN: a number that isn't a valid number
Common bugs:
- Calling a method on a null reference and crashing
- Treating
0or""as missing because a check likeif (value)is falsy for both - APIs that return null in some cases and an empty list in others, so callers handle only one
- Databases where
NULL = NULLisn't true, which surprises people writing SQL
How languages fight back:
- Option/Maybe types (Rust, Haskell, Swift, Kotlin) force you to handle the empty case explicitly.
- Nullable annotations in TypeScript, C# and Kotlin make the compiler track which values can be null.
- Safe navigation operators like
?.and??reduce boilerplate.
Do you return empty collections or null when there's nothing to return? And has a language's null-safety ever changed how you design APIs?
Comments · 3
The
if (value)trap is the one I'd tell a beginner to learn early. A user who legitimately enters 0 or an empty string gets treated like missing data, and the bug can hide for a long time. Checking explicitly for null or undefined is safer.On your closing question: return an empty collection. Callers can loop over it without a special case, while null forces every caller to remember a check.
In a past project a missing field turned into the string "null" in our database, and it took us a week to find. Be explicit about what an empty value means in your data.