← All writing

A line nobody ever drew

Linkvault had code that worked out how many page pictures it keeps on your device and wrote it on the Settings page. The Settings page had no place for it. A 40-line script found it in a second.

·2 min read

Linkvault is my tab saver. It keeps small pictures of pages on your device so the grid looks nice, and Settings has a button to clear them.

Next to that button there was meant to be a line: “214 pictures, 3.2 MB on this device”. The code for it was all there. A function counted the pictures, added up their size, wrote the sentence, and put it in the element named thumbInfo.

There was no element named thumbInfo. Not in any page, not in any template. The function ran every time Settings opened, looked for it, found nothing, and left quietly.

Why nothing complained

The function starts with a guard: if the element isn’t there, return. That’s a normal, careful line. Settings is drawn in sections, and the function shouldn’t crash when its section isn’t on screen.

But that guard also hides the case where the element never exists. Careful code and dead code look the same from outside. All 216 tests passed, because no test asked “is this sentence on the page?”. You don’t write a test for a thing you believe is already there.

Finding it without knowing it

I wasn’t looking for this. I was scanning two of my apps for a general kind of mistake: a script that looks up something no page defines.

The scan is small:

  1. read every script and page in the project
  2. collect every id that is defined anywhere (id="…", and ids built from a prefix)
  3. collect every id a script looks up
  4. print the ones in the second list that aren’t in the first

On Linkvault: 177 ids defined, one looked up and never defined. That one.

On Keyhaven, my password manager, which is much bigger: 292 ids, zero missing.

The fix

One optional field on a settings row, “this row has a line the app fills in”, and the row draws an empty line with that id. The counting function, unchanged, now finds it. Plus a test that the line is on the page, so it can’t quietly disappear again.

That’s version 0.11.4. The whole change is a handful of lines.

Why I like this kind of check

It’s dumb, and that’s its strength. It doesn’t understand the app. It can’t be fooled by a reasonable-looking guard. It only knows that a name is used in one place and made in no place.

I ran the same idea on my Discord bot’s website from the other side: every action a page asks the server for, checked against the actions the server knows. 252 actions, all there.

Not every scan finds something. The ones that find nothing still tell me which kind of mistake I’m not making.