Skip to content

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).total

Sprout, 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?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.