Queen of Code Logo
Melissa BenuaQueen of Code
Back to all posts

When is a Null Not a Null?

Exploring the subtle differences in null handling across programming languages and databases

March 15, 2020
2 min read
Programming
Databases
Data Types
Best Practices
When is a Null Not a Null?
Melissa Benua

Melissa Benua

Engineering Leader & Speaker

Null is one of the most fundamental concepts in programming, yet it's also one of the most misunderstood. What exactly does "null" mean? The answer is more complex than you might think.

The Many Faces of Null

In different programming languages and database systems, null can represent:

  • A pointer that doesn't point to anything (C/C++)
  • An object reference that doesn't refer to any object (Java/C#)
  • An absent value (SQL)
  • An undefined value (JavaScript)

Each of these interpretations brings its own set of behaviors and potential pitfalls.

The Three-Valued Logic Problem

In database systems, null introduces three-valued logic: true, false, and unknown. This complicates queries in ways that surprise even experienced developers:

-- This query might not return what you expect
SELECT * FROM users WHERE age != 25;

The above query won't return users where age is NULL, because NULL != 25 evaluates to "unknown," not "true."

Null vs. Empty vs. Zero

Another common source of confusion is the distinction between:

  • Null (absence of a value)
  • Empty (value exists but contains nothing)
  • Zero (a specific numeric value)

These are fundamentally different concepts, but they're often used interchangeably, leading to bugs.

Defensive Programming Against Nulls

To protect your code from null-related issues:

  1. Use nullable type annotations when available
  2. Implement null object pattern for cleaner code
  3. Leverage Option/Maybe monads in functional programming
  4. Be explicit about null handling in database queries
  5. Consider null coalescing operators (??, ?., etc.) for concise null checks

Conclusion

Understanding the nuanced behavior of null across different contexts is essential for writing robust code. The next time you encounter null, ask yourself: what kind of null am I dealing with, and what are the implications?

Related Posts

Building Teams That Thrive

Key strategies for creating engineering teams that are resilient, innovative, and focused on outcomes.

The Evolution of Engineering Leadership

How the role of engineering leaders has transformed in the era of remote work and AI-assisted development.