---
name: lean-code
description: Minimal-change rules for edits that stay small, stay readable and stop breaking unrelated things. Use before and during any code change, fix or feature.
---

# Lean Code: Minimal-Change Rules

The cheapest bug is the one you never wrote. Every line you add is a line someone has to read, test and maintain. This skill keeps changes small on purpose.

## The core rule

Change the smallest amount of code that fixes the problem properly. Not the smallest amount that makes the test pass. The smallest amount that is correct.

## Before you touch anything

1. Reproduce the problem first. If you cannot reproduce it, you do not understand it yet, and any edit is a guess.
2. Read the code around the problem. Ten minutes of reading saves an hour of reverting.
3. Say the fix out loud in one sentence: "The date parser assumes UTC, so local times shift by the offset." If you cannot say it in one sentence, you have not found the bug yet.

## While you change code

- One change, one reason. A bug fix does not also rename variables, reformat a file or upgrade a dependency. Those are separate commits, separate reviews.
- Do not rewrite a function to fix a line. If the function is genuinely unreadable, that is a different job, and you say so instead of smuggling a rewrite into a fix.
- Prefer deleting code to adding it. A branch nobody hits is not safety, it is clutter. If a path is dead, remove it and let the tests prove it was dead.
- Do not add a configuration option for a value that only has one correct value. Options are not free. Each one is a thing to document, test and get wrong.
- Do not add a layer of abstraction for two cases. Two similar blocks of code are fine. Three is a pattern worth extracting. The rule of three keeps you from building a framework for a problem that only happened once.
- Keep functions short enough to read without scrolling. If a function does three things, it is three functions wearing a trench coat. Split it where the ideas split, not where the lines happen to fall.
- Name things for what they are, not what they do in this one call site. `getUserById` is a name. `doStuff` is a confession.

## The diff test

Before you commit, read the whole diff as if a stranger wrote it. Ask three questions:

1. Could a reviewer understand why each changed line exists?
2. Is there a line here that does nothing for this change?
3. Would this diff still make sense if the commit message disappeared?

If the answer to 1 or 3 is no, the diff is too big or too vague. Split it or rewrite the message. If the answer to 2 is yes, delete the line.

## What to do instead of adding code

- A bug caused by a wrong assumption is fixed by correcting the assumption, not by adding a special case on top of it.
- A missing feature that one user asked for once is usually a question, not a ticket. Ask what they were trying to do before building it.
- A slow function is usually doing unnecessary work. Profile before you optimise. The hot spot is almost never where you think it is.

## Dependencies

- A new dependency is a new relationship. Check its size, its maintenance status and its own dependencies before adding it. A 20-line helper you can write yourself is not worth a package.
- Pin versions. "Latest" is not a version, it is a surprise scheduled for later.

## When the change has to be big

Sometimes it does. A migration, a protocol change, a real rewrite. Then the rule changes shape, it does not disappear:

- Split the work into steps that each leave the codebase working. No step should break the build.
- Land the steps in order. Reviewers read diffs in order too.
- Write the reason for the big change in the first commit message, so nobody has to reconstruct it from archaeology six months later.
