Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Don’t Hoard Code

In the beginning I saved code. A function that parsed something, a clever loop, a module from a project that ended. I’ll use that some day. I didn’t.

With Rust especially, the code is a way of doing things. The way is what’s worth keeping, and the way lives in the spec and the tests, not in the lines.

The next model reads only the page

Here is the reason this matters now. The code for your project will be written, and rewritten, by a model. Not the session you planned it with, but another one: a fresh session, maybe a different model, maybe next year. It wasn’t in the meeting. It didn’t hear you explain what you meant. It has the spec, the tests and whatever else you put in front of it, and nothing more.

Whatever the page doesn’t say, that model will decide for itself. It may not tell you it’s guessing. It will write code that runs, reads well and does something you never agreed to.

So the spec has one job above the others: a model that has never seen this project should be able to produce correct code from the spec and the tests alone. If it can’t, the spec isn’t finished.

What handwaving looks like

Handwaving is a line in the spec that sounds like a requirement and decides nothing:

  • “Handle errors appropriately.” Which errors? What happens to the input? What does the user see?
  • “Fast enough.” What number, measured how?
  • “Works like the old system.” Which behaviours, written out one by one?
  • “The usual validation.” Which rules?
  • A list that ends in “etc.”
  • “TBD” still in the spec when it reaches the gate.

Each one is a place where the next model will make a choice on your behalf.

What specific looks like

jemage’s project file has lines like these:

  • “Duplicate keys and byte offsets are first-class.”
  • “Tolerate any madness in input except circular references.”
  • “Loss of data is tragic.”
  • “Never give two things the same name.”

Each is one sentence, and each settles questions in code that nobody wrote a separate rule for. Should a duplicate key be dropped, merged or kept? Kept. Should a strange input be rejected? Not unless it’s circular. Should a long value be cut short to fit the display? Not if anything is lost.

A spec that can regenerate code needs:

  • The purpose and the reader. Who is this for?
  • Inputs and outputs, with examples. Real ones, including the ugly ones.
  • Each rule stated, with a test. The test is the rule in a form a model can’t misread.
  • What’s out of scope. Otherwise the next model may helpfully build it.
  • The decisions and their reasons. A deliberate choice with no reason written next to it looks like a mistake, and the next model may fix it.

Check it by regenerating

You can test a spec the same way you test code. Take one piece of the project, give a fresh session only the spec and the tests, and ask it to write that piece. Then compare.

If the new code passes the tests and does what you meant, the spec is good. If it differs, the difference shows you where the spec is thin. Fix the spec, not the code. This costs one session, and it’s much cheaper than finding the same gap after the old code is gone.

What to keep

Keep the spec, the tests, the decisions and their reasons, and commit messages that say why.

Let go of the snippets you’re saving for some day, and of generated code you’re afraid to touch. If the spec and the tests are good, the code can be written again. If they aren’t, keeping the old code won’t supply them: it records what the code does, not what it was supposed to do.

In short

Write the spec for a model that wasn’t there. If it can rebuild the code from the page, you can let the code go.

Divider: the gate at dawn. A thin horizontal band with a small pointed gold gate at the centre and a sun showing through it, flanked by two eight-point stars. On each side a band of interlaced gold strapwork in the Islamic girih style, on a Penrose tiling, fades out toward the ends.