Topic 4 of 5 · 5 min read
Small safe refactors with green tests
Goal: Plan a legacy refactor as a sequence of small steps that each keep the tests green.
Refactor in small mechanical steps, run the tests after every step, and commit each step on its own. Then any failure points at the last small change, not at a pile of them.
Check yourself · 1 of 3
Why commit a legacy refactor in small steps with a test run after each?
Why it matters
Agents are happy to rewrite a 600-line function in one go. The result is a big diff that mixes moves, renames and logic changes. If a test fails, you cannot tell which part caused it. If no test fails, you cannot tell what silently changed. Martin Fowler's refactoring catalogue is built on the opposite habit: tiny steps that each preserve behaviour.
How to do it
- Start from green. Run the characterization tests first. If they fail before you begin, fix that before any refactor.
- Write the plan as steps. Ask the agent for a numbered list where each step is one named refactoring: rename a variable, extract a function, move a function, inline a variable.
- One step at a time. Tell the agent to do step 1 only, then stop.
- Run the tests. If they pass, commit with a clear message:
refactor: extract calcTax from checkout. If they fail, revert the step and look at it. - Keep behaviour changes out. A refactor changes structure, not results. If you spot a bug, write it down and fix it in a separate commit.
- Repeat. For a whole system, the strangler fig approach applies the same idea at larger scale: build new pieces beside the old and move traffic across a little at a time.
Before and after
Weak request: "Refactor checkout.js to be cleaner."
Better request:
Plan the refactor of checkout() as numbered steps. Each step is one
mechanical change (extract, rename, move), small enough for a 30-line
diff, and must not change behaviour. Do NOT implement yet. After I
approve, do step 1 only, run the tests, and stop.
A good step looks like this in a diff:
- const tax = subtotal * 0.19;
- const total = subtotal + tax;
+ const total = addTax(subtotal);
with addTax added as a new function whose body is the old two lines, unchanged.
What goes wrong
- The sneaky behaviour change. While "extracting", the agent also tidies a condition, drops a null check or reorders operations. Read each diff for anything that is not a move or rename.
- Edited tests. A refactor that needs test changes is not behaviour preserving. Treat edited tests as a red flag.
- Batching steps. Five steps in one commit puts you back to a big diff.
- No tests, no refactor. Without pinned behaviour, "the tests pass" means nothing.
Examples
One step, one commit
Step 1 extracts addTax. The diff is 8 lines, the suite passes, the commit message says refactor: extract addTax. Step 2 renames t to subtotal. If step 2 broke something, git revert removes exactly that rename and nothing else.
The hidden change
An "extract function" diff also changes if (qty > 0) to if (qty). The tests pass because no test uses a negative quantity. The reviewer spots the condition change because the diff was one step, and asks for it to be reverted into its own commit with a test.
Key points
- Start from green: run the characterization tests before the first change.
- Break the refactor into named mechanical steps small enough to review at a glance.
- Run the tests and commit after every step; revert a step that fails instead of patching on top.
- A refactor changes structure only; fix any bug you find in a separate commit.
- If a refactor step needs test edits, it has changed behaviour and needs a closer look.
Common mistakes
- Asking for a whole-file cleanup in a single prompt.
- Combining a bug fix with a refactor in the same commit.
- Changing tests to make a refactor pass.
- Skipping the test run on steps that look trivial.
Check yourself · 2 of 3
During an "extract function" refactor, an agent also changes if (qty > 0) to if (qty). What is the best response?
Check yourself · 3 of 3
A refactor step passes only after the agent edits an existing test. What does that suggest?
Practice items, not counted towards your mastery.
Start here
- Legacy Modernization meets GenAI (martinfowler.com) (opens in a new tab)ArticleFree
Thoughtworks field notes on using generative AI to understand and modernise old systems.
- Characterization test (Wikipedia) (opens in a new tab)ArticleFree
Defines the technique of pinning current behaviour with tests before you change code.
- Strangler Fig Application (Martin Fowler) (opens in a new tab)ArticleFree
The standard pattern for replacing a legacy system piece by piece instead of all at once.
More for this topic
- Refactoring (Martin Fowler) (opens in a new tab)BookFree
Catalogue of small, behaviour-preserving refactoring steps and why to take them one at a time.