Boy scout rule
The boy scout rule is a simple heuristic for keeping a codebase healthy over time. Its instruction is to leave the campground cleaner than you found it. Applied to software, it means that whenever you touch code you should leave it in a better state than it was in before.
Without deliberate care, code quality degrades as a codebase evolves. Each change made under time pressure tends to introduce a little cruft, and that cruft accumulates as technical debt. The boy scout rule resists this drift by making refactoring a continuous, opportunistic activity rather than something that happens only in dedicated cleanup phases.
The rule is most often practiced as opportunistic refactoring. When you see code that is not as clear as it could be, fix it right then and there. Rename a misleading variable. Extract a small function. Tidy a tangled conditional. These are small acts, but compounded across every commit they keep a codebase habitable and aligned with its code conventions.
The discipline is in the framing. Every commit is an opportunity to either degrade or improve the codebase. Aim for the latter. The goal is not to refactor everything you encounter, but to ensure that each change you make moves the surrounding code a small step toward clean code.
The rule is closely associated with the software craftsmanship movement, which frames continuous improvement as a professional duty rather than a separate cleanup phase.