Lesson 8 of 14 · 20m
Legacy Code: Changing What You Didn't Write
Pin what the code does now, find the seam, change behind it — Feathers' playbook for code with no net.
Michael Feathers defined it in 2004 in Working Effectively with Legacy Code: legacy code is code without tests. Not old code — code you can't change safely, because nothing tells you when you've changed it wrong. The book is still the practical playbook, and the order of operations is the part people skip: get a net under the code, then change it.
Characterization tests first
A characterization test doesn't assert what the code should do — it pins what the code currently does, bugs included. You write them until the risky path is covered, then refactor behind the net. If the function returns the wrong thing for leap years, pin the wrong thing, refactor, and fix the leap year in a separate commit with a new test that asserts the right thing. Never mix behavior-pinning and behavior-changing in one diff — a reviewer can't tell which red bar is the intentional one.
For code whose output is a blob — a serializer, a formatter, a report — the same idea has a cheaper form: the golden master. Run the current code on a fixed input, save the output verbatim, and diff future runs against it. Approval-testing libraries (approvals, cram, your snapshot framework) formalize it. You don't need to understand what the bytes mean to know they changed.
Seams
Feathers' other word: a seam is a place where you can alter behavior without editing that spot. In object languages the seam is usually the object boundary — extract an interface or pass the collaborator in. In C, the link seam substitutes a different object file at build time. In scripting languages, a passed-in function or a patched module import does it. The rule: never edit under the net without a seam, and never cut a seam wider than the change needs.
# before: untestable — hard-wires the world
def invoice_total(order_id):
db = connect(os.environ["DB_URL"])
return db.fetch(order_id).total
# after: a seam — the test substitutes fetch_order
def invoice_total(order_id, fetch_order):
return fetch_order(order_id).totalSprout, wrap, strangle
Three moves cover most legacy work. Sprout: write the new behavior as a new function the old code calls — the new part is fully tested because it's new. Wrap: put the legacy call inside a function with the interface you want; callers depend on the wrapper and the wrapper owns the ugliness. Strangler fig — Martin Fowler's name for it, 2004: grow the replacement around the old system, routing calls at the boundary until nothing routes inward, then delete the core.
The alternative is the rewrite, and the record on rewrites is the industry's cautionary tale: Netscape spent three years rewriting Navigator while Internet Explorer took its market, and Joel Spolsky wrote it up in April 2000 as "Things You Should Never Do." Rewrite only when seams genuinely can't exist — a dead runtime, a platform you're abandoning. Otherwise the strangler wins because the system stays shippable at every step.
Find the seam, then cut
The sequence, in order: write the characterization net → locate the seam → make the change behind it → delete the scaffolding. The suite you built to enable the refactor becomes the permanent spec the next person inherits — that's how a codebase stops being legacy.
Try this: pick a function nobody dares touch. Without changing it, write three tests asserting its current behavior — including one that pins a bug. Now describe the smallest seam that would let you fix the bug. That description is the refactor plan.
Pitfalls
- Editing and testing simultaneously means never knowing which edit broke it.
- "Clean it up while we're here" doubles the review surface and halves the odds the change lands.
- The net is permanent infrastructure — deleting characterization tests after the refactor un-inherits the safety you built.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
What is Go? The Language Google Built for Production Engineering
Go is a statically typed, compiled language built at Google in 2009. It compiles to a single binary with no runtime dependency, starts in milliseconds, and handles concurrency through goroutines and channels. Docker, Kubernetes, and Terraform are all written in Go. Here is why.
What is Rust? The Language That Makes Systems Programming Safe
Rust eliminates entire categories of bugs — null pointer dereferences, buffer overflows, data races, use-after-free — at compile time with no runtime overhead. Microsoft, Google, Amazon, and the Linux kernel all use it for systems code.
What is Java? The Language That Runs Minecraft, Your Bank, and Half the Internet
Java has been declared dead approximately forty times since 1995. It still runs Minecraft, Android, LinkedIn, Twitter's backend, and the financial systems that process your paycheck. Here is what the language actually is, how the JVM works, and why it refuses to die.
What is Python? Enterprise, Data Science, and the Backend of the Web
Python runs Instagram, Dropbox, Netflix, Spotify, and Reddit. It trains the models that power modern AI. It processes petabytes of data in enterprise pipelines. And I ignored it for years because I thought it was just for academics. Here is what changed my mind.