The Boy Scout Rule: Leave Code Better Than You Found It
How the simple practice of making small improvements every time you touch code leads to dramatically better codebases over time.
Robert C. Martin popularized a simple rule borrowed from the Boy Scouts of America: “Always leave the campground cleaner than you found it.” Applied to software, this becomes: every time you touch a piece of code, leave it slightly better than you found it.
Why It Works
Codebases don’t degrade overnight. They rot one shortcut at a time. A quick hack here, a copy-paste there, a “TODO: fix later” that never gets fixed. The Boy Scout Rule reverses this entropy with equally small, consistent improvements.
The Math of Small Improvements
If 10 developers each make one tiny improvement per day:
- 1 week: 50 small improvements
- 1 month: ~200 improvements
- 1 year: ~2,500 improvements
No massive refactoring sprint needed. The codebase improves organically.
What “Better” Looks Like
The key is making improvements that are small, safe, and related to the code you’re already touching.
Rename for Clarity
// Before — what does 'd' mean?
function calc(d: number[]): number {
let r = 0;
for (const v of d) r += v;
return r / d.length;
}
// After — one minute of work, infinitely more readable
function calculateAverage(measurements: number[]): number {
let sum = 0;
for (const value of measurements) sum += value;
return sum / measurements.length;
}
Extract a Helper
// Before — date formatting logic inline
function getOrderSummary(order: Order): string {
const d = order.createdAt;
const dateStr = `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`;
return `Order #${order.id} — ${dateStr} — $${order.total}`;
}
// After — extracted the formatting concern
function formatDate(date: Date): string {
return date.toISOString().split('T')[0];
}
function getOrderSummary(order: Order): string {
return `Order #${order.id} — ${formatDate(order.createdAt)} — $${order.total}`;
}
Add a Type
// Before
function processConfig(config: any) {
if (config.retries) {
// ...
}
}
// After — added type safety
interface RetryConfig {
retries: number;
backoffMs: number;
maxBackoffMs: number;
}
function processConfig(config: RetryConfig) {
if (config.retries > 0) {
// ...
}
}
Remove Dead Code
// Before
function fetchUsers() {
// const oldEndpoint = '/api/v1/users'; // deprecated
// const useCache = false; // turned off in 2023
const response = await fetch('/api/v2/users');
return response.json();
}
// After — removed the commented-out noise
function fetchUsers() {
const response = await fetch('/api/v2/users');
return response.json();
}
Rules of Engagement
- Keep changes small: A rename, a type annotation, removing dead code. Not a rewrite.
- Stay in scope: Only improve code you’re already modifying for your task.
- Don’t mix concerns in commits: Separate your feature work from your cleanup. Use a separate commit like
refactor: rename variables in UserService for clarity. - Run the tests: Even tiny changes can break things. Verify before pushing.
- Don’t refactor what you don’t understand: If you’re unsure what code does, don’t change it. Add a comment instead.
What NOT to Do
- Don’t rewrite an entire module while fixing a bug
- Don’t change formatting in files you didn’t otherwise touch (that’s a separate PR)
- Don’t rename things across a huge codebase in a feature branch
- Don’t “improve” code by adding unnecessary abstractions
Team Adoption
The Boy Scout Rule works best when the whole team adopts it:
- Code reviews: Praise small improvements. Don’t demand them, celebrate them.
- CI checks: Automated linting catches formatting, freeing humans to focus on naming and structure.
- Lead by example: When seniors consistently leave code better, juniors follow.
- Track progress: Some teams tag commits with
[cleanup]and celebrate the count monthly.
The Boy Scout Rule isn’t about perfection — it’s about direction. As long as the codebase is getting better, even slowly, you’re winning.
“We are what we repeatedly do. Excellence, then, is not an act, but a habit.” — Aristotle