Case file P6

Leave it Better Than You Found it

Boy Scout, sharper.

Tidy the line that was touched. Cleanups belong in their own commit, not folded into a feature PR. The file already open for the bug fix is the file that gets tidied on the way past. The compounding effect of small, opportunistic cleanups is the corrective for software entropy.

ByAdam LewisPublished3 May 2026Revised13 Oct 2026Reading12 minVersionv1.2ConfidenceHigh
§0b

Opinion

The compound interest on small cleanups is what separates a codebase you can still release from one you can't. The team that tidies one variable per commit ends a year with names that read like English. The team that defers tidying ends a year with the same code plus a year of accreted opacity. Martin's point in Clean Code is the same point, made from the opposite direction: the codebase is always either cleaner or dirtier than it was yesterday. The engineer chooses which.1

The bit I want to plant a flag on is the second sentence of the sub-line: cleanups travel in their own commit, not folded into the feature PR. Beck's Tidy First? is the modern reconciliation of the rule with the PR-discipline of P7 Short-Lived Branches.3 The cleanup belongs in a separate commit, ideally a separate PR, so the reviewer can read the structural change and the behaviour change without one obscuring the other. The rule survives. The diff hygiene survives. Both belong.

Sandi Metz's “Wrong Abstraction” is the qualifier inside the row: not every tidy is an improvement. In her words, “duplication is far cheaper than the wrong abstraction”, so a cleanup that extracts the wrong one leaves the code worse than it found it.4 Treat the rule as opportunistic, not compulsive: the file opened to fix the bug is the file tidied on the way past.7 The file that stayed closed is left alone.

Copy a note and link

Grab this short comment and drop it into a PR comment or an LLM chat to prompt the right change.

Tidy the line you touched. Don't smuggle a refactor into a feature PR. Do tidy the thing you came to fix. Cleanup goes in a separate commit (Beck's Tidy First). Behaviour change in another. Don't refactor what you can't test, and don't let the cleanup turn into a yak shave.

/tenet/leave-it-better/P6
Copy for paste
§0c

AI eyes only

Rule: tidy what you touched, no further. Behaviour changes and tidies travel in separate commits.

Reject: refactoring files unrelated to the requested change. Reject: bundling forty unrelated edits into the behaviour-change PR. Reject: declining to fix a small mess in the file you are already editing.

Generate: make the requested behaviour change first. Then in a separate commit, fix the adjacent rough edges in the same file. Each tidy is the minimum to remove a friction the next reader will feel.

Diagnostic: a reviewer should distinguish behaviour change from tidy in five seconds. If the diff mixes them, split.

§0d

Why?

  • Compounding works. One small cleanup per commit ends a year with names that read like English. Deferring ends a year with the same code plus a year of accreted opacity.
  • The cost is incidental. The unit is small (rename one variable, split one function) and the trigger is incidental: tidy only the file you opened to change.
  • Beck's Tidy First? is the modern reconciliation: tidy in a separate commit, separate PR if large. The rule survives. The merge-conflict tax goes down.
  • Pair with P1 Test-First Development. A green suite is a refactoring licence. Opportunistic cleanups stop being a leap of faith.
  • The diff stays clean. The reviewer reads the tidy diff in two minutes, the feature diff in two minutes. Merge conflicts don't compound across feature and structural changes.
  • Hunt and Thomas's “broken windows” idea in reverse: every cleaned window deters the next break. The codebase signal that says “we keep this clean” is what stops the next entropy increment.
  • No cleanup sprints. The team that tidies as it goes never has to schedule the dedicated refactor week. The dedicated refactor week is the sign that the rule was skipped for a year.
The receipts
Origins, quoted passages, evidence, the strongest counter-argument and the reply.
§1

Origins

The phrase belongs to Robert Baden-Powell, founder of the Scout Movement, whose 1941 farewell message asked Scouts to “try and leave this world a little better than you found it.” The campsite version, leave it cleaner than you found it, softened over the next half-century into the Scouting tradition every modern engineer who hears the term has half-met somewhere.

The software adaptation belongs to Robert C. Martin. Clean Code: A Handbook of Agile Software Craftsmanship (Prentice Hall, 2008) ch.1 page 14 has the canonical formulation: if we all checked in our code a little cleaner than when we checked it out.1 Martin re-published a one-page version as Essay 8 of 97 Things Every Programmer Should Know (O'Reilly, 2010), which is the link most engineers read.

The older intellectual root is Hunt and Thomas's “broken windows” ( The Pragmatic Programmer, 1999; Topic 3 “Software Entropy” in the 2019 edition).2 Hunt and Thomas wrote it as a prohibition with the action attached: don't live with broken windows, and fix bad designs, wrong decisions and poor code when you see them. Martin's rule scopes that action to the code being checked in: leave it a little cleaner than it was checked out. The two pieces fit together as one discipline.

Kent Beck's 2024 Tidy First? is the modern reconciliation.3 Beck's reply to the diff-hygiene critique: tidyings and behaviour changes belong in different commits, possibly different PRs. The rule survives. The merge-conflict tax goes down. Pair this with P7 Short-Lived Branches with AI+Human Review and the practice is operationalised: one PR, one behavioural change, plus a separate PR per structural tidy.

Sandi Metz's “Wrong Abstraction” (2016) is the qualifier inside the row.4 Duplication is far cheaper than the wrong abstraction, so not every cleanup leaves the code better. Metz's pragmatism is the corrective to compulsive cleanup. Treat the rule as opportunistic, not mandatory.

§2

Quotes

The Boy Scouts of America have a simple rule that we can apply to our profession. Leave the campground cleaner than you found it. If we all checked-in our code a little cleaner than when we checked it out, the code simply could not rot.

Robert C. Martin · Clean Code (2008)

Don’t Live with Broken Windows … Fix bad designs, wrong decisions, and poor code when you see them.

Andy Hunt & Dave Thomas · The Pragmatic Programmer (1999)

TF? distinguishes structure changes and behavior changes. … Constrain PRs to only contain one kind of change or the other. Never both.

Kent Beck · Structure & Behavior PRs (2021)

prefer duplication over the wrong abstraction … Once an abstraction is proved wrong the best strategy is to re-introduce duplication and let it show you what’s right.

Sandi Metz · The Wrong Abstraction (2016)
§3

Evidence

19 external sources, ranked by author authority. The first five are the canon; expand to see the rest, including the qualifiers and the named opposers. Each links out to its primary source.

  1. 01
    Robert C. Martin · 2008
    The canonical formulation: always check in a module cleaner than when you checked it out. Cites Baden-Powell as the source.
  2. 02
    Robert C. Martin · 2010
    One-page version of the Clean Code rule. The link most engineers read; CC-BY mirror in circulation.
  3. 03
    Andy Hunt & Dave Thomas · 1999, 2019
    A prohibition with the action attached: don't live with broken windows; fix bad designs, wrong decisions and poor code when you see them. The intellectual root of Martin's rule.
  4. 04
    Kent Beck · 2024
    The modern reconciliation. Tidyings and behaviour changes belong in different commits. The rule survives, the merge-conflict tax goes down.
  5. 05
    Martin Fowler · 2018
    The vocabulary the rule operates in. Each refactoring (Extract Function, Rename Variable, Inline) is a specific tidy the rule names.
  1. 06
    Martin Fowler · 2011
    Names the practice the rule describes. Warns of yak-shaving and recommends “good judgement” on when to stop.
  2. 07
    Sandi Metz · 2016
    Duplication is far cheaper than the wrong abstraction, so a cleanup that extracts the wrong one leaves the code worse. The qualifier inside the row.
  3. 08
    Sandi Metz · 2012, 2018
    Metz's wider refactoring-with-pragmatism teaching. Code that doesn't churn shouldn't be touched. Cleanup has a cost.
  4. 09
    Michael Feathers · 2004
    The legacy-code corrective: refactoring without a regression suite is walking a tightrope without a net. Pair the rule with P1.
  5. 10
    Google · 2019
    Refactorings usually belong in a separate CL from feature changes or bug fixes, because reviewers understand each change more easily when they are separate. Small cleanups such as fixing a local variable name may stay inside the feature CL.
  6. 11
    Linux Kernel community · 2010s
    “Separate each logical change into a separate patch.” The kernel's diff-hygiene rule that the Boy Scout rule has to negotiate around.
  7. 12
    Kent Beck · 2012
    “For each desired change, make the change easy (warning: this may be hard), then make the easy change.” The preparatory-refactoring idea: close cousin of the Boy Scout rule.
  8. 13
    Martin Fowler · 2015
    Operational walk-through of Beck's tweet. The cleanup precedes the feature, not accompanies it. An alternative pattern to the in-place Boy Scout cleanup.
  9. 14
    Anton Pastukhov · 2025
    Practitioner critique of the rule's diff-creep failure mode. Useful as the qualifier the modern Tidy First reconciliation answers.
  10. 15
    Francisco Vilches · 2021
    PR-level qualification of the rule. The cleanup that bloats the PR is a cleanup the team won't merge.
  11. 16
    Jason Swett · 2018
    Argues the rule alone doesn't pay for the technical debt it nudges at. Useful as the broader-context qualifier. Pair with dedicated refactor budget.
  12. 17
    refactoring.guru (after Fowler & Beck) · 2024
    Modern practitioner-friendly entry point to the refactoring vocabulary: a catalogue of code smells and the refactoring techniques that address them.
  13. 18
    Alberto Bacchelli & Christian Bird · 2013
    Code improvements (readability, consistency, dead-code removal) were the most frequent kind of review comment in Microsoft code reviews: 165 of the 570 comments classified. Evidence that reviewers already ask for small cleanups; the paper does not discuss the Boy Scout rule.
  14. 19
    Jørn Ølmheim · 2010
    Companion essay in the same volume as Martin's Boy Scout entry. Argues that beautiful code is simple code; it does not discuss cleanup.

Nineteen sources, three stances. The supporters are Martin, Hunt & Thomas and Fowler: the cleanup canon. The qualifiers further down push the line that the rule needs nuance: not all code is worth tidying, tidy in separate commits, do not refactor without tests. The opposer is the Linux kernel's patch discipline: one logical change per patch, each one easy for reviewers to understand and verify. The steelman the case has to address.

§4

Examples

Viewing: TypeScript.
Avoid

Before new feature goes out; the adjacent pyramid stays.

Filesanctuary-log.ts
function validateHedgehogRecord(hg: Hedgehog | null): boolean {
  if (hg) {
    if (hg.weightG >= 600) {
      return true;
    }
  }
  return false;
}

function recordHibernation(hg: Hedgehog, log: HibernationLog): void {
  if (validateHedgehogRecord(hg)) {
    log.entries.push({ id: hg.id, since: now });
  }
}
Prefer

After same feature; three lines of guard clauses on the line we touched.

Filesanctuary-log.ts
function validateHedgehogRecord(hg: Hedgehog | null): boolean {
  if (!hg) return false;
  if (hg.weightG < 600) return false;
  return true;
}

function recordHibernation(hg: Hedgehog, log: HibernationLog): void {
  if (validateHedgehogRecord(hg)) {
    log.entries.push({ id: hg.id, since: now });
  }
}
§4b

Enforcement

Viewing: TypeScript.

Apply these rules in eslint.config.mjs. The full enforcement across every tenet lives on the implementation page.

RuleToolCatches
unused-imports/no-unused-importseslint-plugin-unused-importsimports left after a refactor: the most common cleanup signal. Auto-fixes; the engineer never opens a fix-only PR.
unused-imports/no-unused-varseslint-plugin-unused-importsvariables declared and never used. Stops the dead-state-on-the-way-past pattern from compounding.
no-unreachableESLint corecode after return / throw: the dead-code signal the rule asks the engineer to clean.
no-emptyESLint coreempty blocks left after refactor. Catches the “half-cleanup” shape; either restore behaviour or remove the block.
Husky commit-msg (one-type hook)Husky commit-msgcommit headers that do not name exactly one Conventional Commits type, so every commit declares its kind: refactor for a tidy, feat or fix for a behaviour change. It reads the message, not the diff, so a commit that mixes the two under one type still needs the reviewer.
Conventional CommitsConventional Commitsthe commit-type taxonomy: feat / fix for behaviour, refactor / chore for structure. The vocabulary the hook reads.
@typescript-eslint/no-unused-varstypescript-eslintunused parameters and variables in TS source.
eslint.config.mjsconfiguration snippet
import { defineConfig } from 'eslint/config';
import tseslint from 'typescript-eslint';
import unusedImports from 'eslint-plugin-unused-imports';

export default defineConfig({
  files: ['**/*.{ts,tsx}'],
  languageOptions: { parser: tseslint.parser },
  plugins: { 'unused-imports': unusedImports },
  rules: {
    'unused-imports/no-unused-imports': 'error',
    'unused-imports/no-unused-vars': 'error',
    'no-unreachable': 'error',
    'no-empty': ['error', { allowEmptyCatch: false }],
  }
});
§4c

AI rules

File.cursor/rules/p6-leave-it-better.mdc
---
alwaysApply: true
---

## Prickles P6: Leave it Better Than You Found it

Tidy the line you touched. The unit is small: rename one variable, split one function, delete one dead import. The trigger is incidental: tidy only the file you opened to change.

Tidy in a separate commit. Beck's Tidy First rule: structural change and behaviour change belong in different commits, possibly different PRs.

Don't refactor what you can't test. A green suite is a refactoring licence. Without one, the cleanup is walking a tightrope without a net.

Don't let the cleanup grow into a yak shave. Fowler's “opportunistic refactoring” warning: every cleanup reveals another. Use good judgment on when to stop.

Repo layout, CI, and ESLint wiring for these paths live on /implementation, not repeated on every tenet.

§5

Counter-argument

Counter

The strongest steelman is the diff-hygiene critique. The Linux kernel's Submitting Patches docs ask for each logical change in its own patch, and Google's engineering practices recommend refactorings in a separate CL from feature changes or bug fixes.5 Both give the same reason: a reviewer understands each change more easily when the changes are separate. Sandi Metz's “Wrong Abstraction” sharpens the point: duplication is far cheaper than the wrong abstraction, and a cleanup that extracts the wrong one leaves the code worse than the original mess.4 Michael Feathers extends the warning into Working Effectively with Legacy Code: refactoring without a regression suite is like “doing aerial gymnastics without a net.”6

§6

Counter-argument retort

Reply

The diff-hygiene critique is sharpest when the cleanup obscures the behavioural change. Beck's Tidy First? answers the critique without abandoning the rule: tidy in a separate PR, never in the same diff that releases the feature.3 The reviewer reads the tidy diff in two minutes (no behavioural change, just structure) and the feature diff in two minutes (no structural change, just behaviour), and both PRs merge cleanly.

Metz's “Wrong Abstraction” is the second corrective. The compounding effect of small cleanups is real. The compounding effect of bad abstractions imposed in cleanup is worse. Apply the rule to names, splits, comment removals and dead-code deletions.8 Do not “introduce an abstraction” opportunistically. Pair this with A2 Three-Tier Hoisting and the rule of three: duplication earns its abstraction. Opportunistic abstraction is debt.

Feathers' legacy-code warning is the third: don't refactor what you can't test.6 The cleanup that breaks the production system is worse than the entropy it was trying to fix. The fix is not to skip cleanups. It is to pair the rule with P1 Test-First Development. A green suite is a refactoring licence. Without one, the Boy Scout walks the tightrope without a net.

In production work, the row that tidies one thing per commit produces a codebase the next engineer can deliver in. The row that doesn't produces a codebase the next engineer complains about and doesn't fix, on the grounds that the diff would be too large. Compounding works in both directions. The rule decides which.

§7

Notes

  1. [1]Robert C. Martin: Clean Code: A Handbook of Agile Software Craftsmanship (Prentice Hall, 2008), ch.1 page 14. The canonical formulation: always check in a module cleaner than when you checked it out. Re-published as Essay 8 of 97 Things Every Programmer Should Know (O’Reilly, 2010).
  2. [2]Andy Hunt & Dave Thomas: The Pragmatic Programmer, 20th Anniversary Edition (Addison-Wesley, 2019), Topic 3 “Software Entropy”. Tip 5 “Don't Live with Broken Windows”: “Fix bad designs, wrong decisions, and poor code when you see them.” Martin's rule scopes that action to the code being checked in.
  3. [3]Kent Beck: Tidy First? A Personal Exercise in Empirical Software Design (O'Reilly, 2024). The modern reconciliation: tidyings and behaviour changes belong in different commits, possibly different PRs. The rule survives and the merge-conflict tax goes down.
Disagree? Found a hole in the argument? Take issue with this tenet →
Last revised: 2026-10-13