Furthermore: NULL
isn’t truthy, unknown, undefined, nonexistent, nil, or zero
Some languages are misleading coders about whatNULL
means. A recent project highlighted just how tricky this can be. For example in JavaScript:
let maybeNull = null;
let isNull = maybeNull === null;
let isntNull = maybeNull != null;
console.log({isNull, isntNull}); // {isNull:true, isntNull:false}
JavaScript seems to provide the intuitive answer at first glance. However, as we move forward, you’ll see that this could be misleading. For example, once you connect to a Postgres database:
Do not write
expression = NULL
becauseNULL
is not “equal to”NULL
. (The null value represents an unknown value, and it is not known whether two unknown values are equal.)
— Postgres Docs
Therefore, attempts to compare NULL
in Postgres always return NULL
(which your JavaScript code may interpret as always false). Since the purpose of a database is to record known information, it seems reasonable that Postgres treats NULL
as unknown. Yet this still doesn’t work as a universal definition. For example in GraphQL:
{
"data": { "myData":NULL },
"errors": [ ... ]
}
The myData
property returned as NULL
. This doesn’t mean that myData
is unknown to the server. Perhaps GraphQL returned NULL
due to authentication or throttle error(s). The value may be known and explicitly defined; you simply need to modify/retry your query.
You might be tempted to test myData === null
but this seems like an anti-pattern. Depending on your programming language, the test may always yield false
! Furthermore, this doesn’t tell you whether you need to modify/retry your query. Better to resolve the errors and parse the expected type first (nicely done with zod). If you resolved all errors and didn’t obtain an expected type, it is de-factoNULL
(and therefore no need to test it).
Is the dictionary any help?
Some definitions for NULL
are helpful:
Void; of no legal or binding force or validity; of no efficacy; invalid.
Of no account or significance; having no character or expression; negative.
Something that has no force or meaning; that which is of a negative or meaningless character.
However, with regard to programming, some definitions of NULL
can also be misleading:
Not any; wanting; non-existent.
Zero in algebraic measure.
So how should we define it?
My opinion, is something along these lines: NULL
is similar to “I plead the 5th” or an interview non-response due to a non-disclosure agreement. The value will be unknown to you; however this does not mean that it is unknown to the discloser. It may be anything, everything, or nothing. It may be known or unknown.
Thinking through database design
Suppose the following Postgres table:
| User | URL |
| ------- | ---------------------------- |
| Bob | https://example.com/bob |
| Betty | https://example.com/betty |
We create new user Pat
before obtaining a URL. Should URL
be NULL
or something else? I’d argue that the default should be UNKNOWN
if possible.
NULL
doesn’t provide the most helpful informationempty string
is not a valid urlVOID
suggests the URL is known (and nonexistent)UNKNOWN
explicitly confirms that this value is not currently known
Now suppose Betty
removes her URL (now known to be nonexistent). It seems VOID
would be most appropriate.