Lesson 3 of 14 · 18m
Characterization Tests: Pin Before You Change
Legacy code is code without tests — pin what it currently does with golden masters and seams before you change what it should do.
You inherit a 40,000-line service nobody on the team wrote, with zero tests, and a ticket that says "change the refund flow." Whatever you ship gets judged against behavior nobody specified — the behavior the code has today, which users already depend on. Michael Feathers named this in Working Effectively with Legacy Code (2004): legacy code isn't old code. It's code without tests — code you can't change safely because you can't tell what you broke.
The wrong response is writing tests for what the code should do. You don't know what it should do. Hyrum's Law — with a sufficient number of users of an API, all observable behaviors of your system will be depended on by somebody — means the weird rounding, the extra whitespace in the CSV, the 500 that should have been a 400: all of it is someone's spec.
The technique: pin, then change
A characterization test asserts what the code currently produces for recorded inputs — not correctness, fidelity. You capture the current output and freeze it:
def test_report_output_is_pinned(report_generator):
inputs = load_cases("fixtures/report_inputs.json") # real production shapes
for case in inputs:
assert report_generator.run(case) == golden_master[case.id]Build the golden master by running the current code over real input shapes and storing the output — testdata/golden/ in Go convention, __snapshots__/ in Jest, cargo insta inline snapshots in Rust. The test doesn't say the output is right. It says the output is the same. The workflow matters: capture before you touch anything, and the inputs should come from production shapes — a sanitized dump of real requests — not from your imagination of what inputs look like.
The capture step
Practically: log or dump a few hundred real inputs from production (scrub PII), run the legacy path over them, store input → output pairs as your golden master, and wire the test to replay them. A hundred pinned cases beats five hand-invented ones — the weird inputs are the point. Size it to the code path you're changing, not the codebase. And expect a few of the captures to be obviously wrong — that's fine, they're pinned as current, and you file them instead of fixing them mid-refactor.
ApprovalTests — Llewellyn Falco's library — packages the same idea with an explicit review step: the run produces received.txt, you diff it against approved.txt, and approving means literally replacing the approved file. The approval stays a human act, which keeps pinning from becoming rubber-stamping.
Seams and sprouts
Feathers' other tool is the seam — a place where you can alter behavior without editing in place. An injected dependency is a seam. A function you extract is a seam. In compiled languages there are link seams (swap the dependency at link time) and preprocessing seams (#ifdef, monkeypatching at test scope); in dynamic languages the seam is usually a parameter or a module attribute you can substitute. Find one near the change, pin around it, then work.
Two moves cover most changes:
- Sprout method. New logic goes in a new function, written under test, called from one line in the legacy code. The untested mass never gains new untested code.
- Wrap method. The change wraps existing behavior — log the call, pre-validate the input — without modifying the gnarly function at all.
Joel Spolsky's 2000 warning is the backdrop: Netscape rewrote the browser instead of refactoring it, the rewrite took three years, and the product rotted in the meantime. Characterization testing is what makes "never rewrite" survivable — you get the safety net the original authors never left.
Field notes
- When the pinned output is wrong, don't fix it silently — file it. Changing a pinned behavior is a decision, not a diff.
- Record-replay HTTP cassettes (
vcrpy, Polly.js) are characterization tests for external APIs: real responses frozen as fixtures. - Coverage of legacy code is a map, not a mandate. Pin the path you're changing, not the whole codebase.
- Refactor under the pin first, then change behavior second — never both in the same commit; the characterization diff is how you know which one you did.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
Your Container Is Running SerialGC and Nobody Told You
You tuned G1 flags for a week. Your pod has one CPU, which means the JVM quietly picked a single-threaded collector at startup and ignored every flag you set. Here is the OpenJDK source that made that decision.
Java Pulled Primitive Patterns After Five Previews. JDK 28 Won't Compile Them.
Primitive types in patterns, instanceof, and switch previewed in every JDK from 23 to 27. On November 9 the Amber team withdrew the feature, and it won't be in JDK 28 even with --enable-preview. Here's why Valhalla changed the plan, what replaces it, and how to rewrite code that used it.
Minecraft Runs on One Thread Because Nobody Wrote It for Two
Every entity, every block update, every redstone pulse — one thread, twenty times a second, 50 milliseconds a pass. Folia's answer is to stop sharing the kitchen. Here is the tick budget and the ownership rules, read from the PaperMC source.
Ask Java for AES and You Get ECB. Nobody Warns You.
Cipher.getInstance("AES") compiles, runs, encrypts, decrypts, and hands you the weakest mode in the box. Here is the OpenJDK source that makes that decision, and the line where ECB gets chosen for you.