Founder Notes
A Constant Is an Invariant You Stopped Checking
I found a hardcoded true this week, decided it was a bug, and then could not
demonstrate that it was wrong. Working out why took longer than the fix would
have, and it changed how I read stored values.
The setup. There is a log that records every change to one particular setting
on a record — one row per change, written at the moment the change happens, so
that someone can reconstruct afterwards what was done and to what. One of the
columns on that log describes the record the change applies to: a flag saying
the record belongs to a certain class.
Both of the write paths that append to this log — the one that turns the
setting on, and the one that turns it off — set that column to a literal true.
Not read from the record. Not passed in by the caller. Typed into the source.
My first read was the obvious one: somebody did not have the value to hand and
filled in the common case. That happens, and it is usually a defect. So I went
looking for the row that would prove it — a log entry whose flag said one thing
while the record it referred to said another.
Why there is no such row
There is a constraint on the data, declared somewhere else entirely, which says
that a record with that setting enabled must also belong to that class. It is
enforced at write time. It has been there since before the log existed.
So every record that can possibly reach either write path already satisfies the
flag. The literal is not an approximation of the common case. It is correct,
for every row that has ever been written and every row that could be written
while the constraint stands. The hardcoded value and a value read honestly from
the record are the same value, always.
I want to be precise about that, because it is the part I had wrong. The code
is not buggy. It produces the right output. If I changed it to read the flag
from the record, the log would come out byte-identical. There is no test I could
write that would fail.
What is actually missing
What the row does not contain is the reason it is true.
The flag is written as though it were an observation about the record. It is not
an observation. It is a conclusion, and the argument for it lives in another
file, in a different language, under a different author, and the row never cites
it. Anyone reading the log reads an assertion about a record. Anyone reading the
write path reads a constant. Neither of them can see that the first depends on
the second, which depends on a rule that neither of them mentions.
Which is fine, right up until the rule changes. Constraints get relaxed for good
reasons — a new class of record needs the setting, a migration needs a window,
someone decides the coupling was always too strict. The person doing that is
looking at the constraint. They are reasoning carefully about what the
constraint permits and what relaxing it would allow. They are not looking at a
literal in an append path elsewhere in the codebase, because nothing connects
the two, and the literal does not announce that it has a dependency.
And then the log keeps writing true. It does not start failing. It does not
warn. It produces rows that look exactly like the correct rows that came before
them and are false. The value outlives its reason, and nothing anywhere
notices, because writing it never required the reason to be present.
The generalisation
A constant is an invariant you have agreed to stop checking.
That is the whole of it. Every hardcoded value that could instead have been
derived is a small bet that something will stay true, and the bet is usually
sound at the moment it is placed. What makes it dangerous is that the bet is not
written down anywhere near the place it pays out. The value has no reason
attached, and therefore no condition under which anyone would think to revisit
it.
So the question I now ask of a literal is not the one I used to ask. I used to
ask whether it was right. It almost always is — that is why it survived review
in the first place. The question that does work is this: what has to remain true
for this to keep being right, and would anyone changing that thing ever find
their way here?
If the answer to the second half is no, then the value is not a fact. It is a
cached inference with no expiry, and the cache does not know what it depends on.