The pull request
added a cache
in front of the price service.
Prices change
once a day,
overnight.
So the cache
should hold them
for a day.
const val PRICE_CACHE_TTL_SECONDS = 8640000L
You read it.
A long number,
lots of zeros,
roughly a day
in seconds.
Approved.
So did the second reviewer.
A day
is 86,400 seconds.
That line
has one zero more.
One hundred days.
Two weeks later
the buyers raised
the price of cement.
The website
did not.
The branches did.
So the counter
and the checkout
disagreed for days
before anyone found the cache.
Here is the mechanism.
Your eyes
do not count digits.
Up to about four,
you see how many things
there are
without counting.
Psychologists call that
subitizing.
Past four,
you have to count,
one by one,
and nobody counts
when they are reviewing
forty files.
So a long run of zeros
is read by shape.
Long number.
Starts with 86.
Must be the day one.
The check you thought you did
was a glance
at the first two digits.
And the word "seconds"
in the name
made it look more careful
than it was.
Here is what to do.
Do not make anyone count.
Most languages
let you group digits:
const val PRICE_CACHE_TTL_SECONDS = 86_400L
Kotlin, Java, Python,
JavaScript, Go, Rust and C#
all accept a separator.
Now the extra zero
breaks the pattern
you can see.
Better still,
do not write the number at all.
val priceCacheTtl = 1.days
Or Duration.ofDays(1) in Java.
A reviewer can read "one day".
Nobody can read
seven zeros.
Or show the arithmetic:
24 * 60 * 60
And when you review
a constant
that controls time,
money or size,
stop
and say its value out loud,
in words.
If you cannot,
you have not read it.
Long numbers
are not read.
They are recognised,
and they are recognised wrong.
– Serguey Asael Shinder
Top comments (0)