DEV Community

Darkø Tasevski
Darkø Tasevski

Posted on Originally published at darkotasevski.dev

The Boy Scout Rule

The Boy Scout rule, leave it better than you found it, is Uncle Bob's version of an old Baden-Powell line, and it shows up in onboarding docs and code reviews everywhere. It's good advice, but it doesn't say anything about size, and I've seen it turn small fixes into big diffs often enough (mine included) that I wanted to write down where I think the line is.

The usual way it goes is something like this. You open a file to fix a one-line bug, the function around it looks messy so you clean it up, cleaning it up means renaming a couple of things, which means touching the callers, which makes a small helper look redundant, so you inline it, and two days later the "one-line fix" ships as a 400-line diff. I did exactly that once, and my reviewer approved it without really knowing what they were approving, mostly because it was me and they trusted me (which is its own problem). A colleague of mine used to describe this spiral with a flat "one thing led to another", usually right before a small PR turned enormous, and he was never wrong.

Why the rule exists

The rule exists for a reason. Code decays by default - people leave, context gets lost, and features pile up on top of assumptions that stopped being true two years ago, so if nobody ever improves anything they touch, the only direction is down. Treating a small mess as something we don't walk past, the same way we wouldn't drop litter, is a healthy instinct for a team to share. Going too far the other way is a problem too, because if we never touch anything every TODO stays a TODO, and the codebase rots in a thousand small messes that everyone walked past because it wasn't "their" PR, so leaving it exactly as you found it is also a decision.

So I'm not against improving code, my problem is with what the rule turns into when nobody asks how much.

Where it goes wrong

The trouble is that "leave it better" has no size limit built in, so it covers any amount of work as long as we can call it an improvement, and there are a few ways this usually shows up.

The first one is the bugfix that becomes a refactor, like the story above. The surrounding code annoys you, and since you're already in the file it feels efficient to fix that too, which it rarely is, because the fix was done in the first ten minutes and everything after that was free time inviting more work in. That's the Parkinson's Law pattern again, the task expands to fill the time we give it, and "while I'm in here" is a lot of time.

The second one is what that does to the review. A tidy bugfix is a 30-line PR someone can properly review, and the same bugfix with cleanup bolted on is a 400-line PR they rubber-stamp. I've written about how big diffs wreck reviews in the CI post - once a PR crosses some size the review turns into a vibe check, so the cleanup doesn't only risk the change you smuggled in, it also weakens the scrutiny on the actual fix, which was the urgent part. We end up with prettier code and a worse review.

The third one is the most tempting. While cleaning up you spot three things that are almost the same and unify them, and now there's a shared helper, a config flag or a base class. It feels like leaving it better, but often we've just added an abstraction the code didn't need and made the next person's change harder (that's more or less the whole argument in Too Much Abstraction), and better and more abstract are not the same thing.

Would it survive as its own PR?

The question I ask now before cleaning anything up while I'm in a file is would this survive as its own PR? If the cleanup, pulled out and titled for what it is (refactor: simplify the X handler), would get reviewed and approved on its own merits, then it's real work and it deserves its own PR, so open one. If the only reason it's getting in is that it's hitching a ride on an urgent bugfix nobody wants to block, it's scope creep and it can wait.

There are a few more checks behind that one:

  • Do you understand this code? Don't refactor what you don't understand, git blame and a quick word with whoever wrote it are cheaper than the bug you'll introduce by guessing at the intent.
  • Is it tested? If the code has no tests, the cleanup is a change you can't verify, so either add the test first (which is the improvement on its own) or leave it alone.
  • What's the blast radius? A local rename inside one function is nearly free, touching a shared signature is not, and the rule works a lot better at the small end than at the big one.

What fits in the same PR

The version of the rule that works is the small one, improvements cheap enough that they don't compete with the change you came to make:

  • A comment that explains the one confusing line, or deleting a stale comment that's been lying to people for a while.
  • A single test, even a dumb one. The first test in a file is the expensive one to write, after that the setup is mostly copy-paste, so leaving that first test behind makes the next one someone else writes nearly free.
  • A local rename or an early return that makes one block read in the order it executes.

None of these blow up the diff or need their own design discussion, and all of them can be reviewed at a glance, which is what the rule was meant for in the first place. So clean things up, but at a size that's honest about what you're shipping - small enough to review, understood, tested, and either in scope or in its own PR.

Top comments (0)