How to test for clean code
I want clean code enforced by a test, not by review.
The rule
Don’t comment your code. Refactor it.
A comment explaining a block of code means the block wants to be a method with a good name. Martin Fowler, in Refactoring: “Even a single line is worth extracting if it needs explanation.”
The rules
I had an LLM compose the article down to rules for my codebase:
- Don’t write a comment that explains what a block of code does.
- Extract that block into a function named after what the comment would have said.
- Extract even a single line if it needs explaining.
- Write code as if comments didn’t exist.
- Allow comments only for API docs, truly complex logic, sparing TODOs, and why a design choice was made.
What I found
My own code breaks the first rule. Code an LLM wrote with me, the same day:
// Newest on top. The two ticks past the last job hold the full inbox before it starts over.
const shown = FOUND.slice(0, Math.min(arrived, FOUND.length)).reverse().slice(0, VISIBLE)
The fitness test
A test that fails when a comment explains code instead of a name.