Legacy systems
Last updated
Every long-lived organisation runs software older than some of its staff: it works, it's essential, and nobody wants to touch it. In this lesson you'll learn what makes code “legacy”, how to change it safely by first pinning down what it does, why rewriting from scratch so often fails, and how the strangler fig pattern replaces an old system a piece at a time. You'll also look at Southwest Airlines' December 2022 meltdown, and what it shows about old systems left too long.
By the end of this lesson you can
- Explain why code without tests is hard to change safely
- Use characterisation tests to pin down what old code does
- Replace a system gradually with the strangler fig pattern, rather than a big rewrite
What makes code legacy
“Legacy” is often used for code that's old, or in an old language. Michael Feathers, in Working Effectively with Legacy Code, gives a sharper definition: legacy code is code without tests.
His reason: without tests, you can't tell whether a change broke something. So every change is risky, so people change as little as possible, so the code gets patched instead of improved, so it gets harder to understand. Code can be legacy the day it's written. Old code with good tests can be changed with confidence.
Pin it down first
Before changing legacy code, Feathers suggests writing characterisation tests: tests that record what the code does now, not what anyone thinks it should do.
- Call the code with some input.
- Write an assertion you know is wrong, such as
assert result == "?". - Run it. The failure tells you the actual result. Put that in the assertion.
- Repeat for the cases you're about to touch.
Now you have a safety net. If a characterisation test turns up something odd, like a fee that's rounded the wrong way, don't fix it silently: someone may depend on it. Note it, ask, and fix it as a separate, deliberate change.
The rewrite
Faced with a tangled old system, teams are tempted to throw it away and start again. In a well-known essay from 2000, Joel Spolsky called this “the single worst strategic mistake” a software company can make, pointing to Netscape, whose browser rewrite took about three years while competitors moved ahead.
His argument: old code looks ugly partly because it's full of fixes, each one a bug someone found in real use. Like Pila's phone[-10:] line (lesson 8.2). A rewrite throws those away and has to rediscover them, while the old system still needs maintaining and users wait for features.
Rewrites aren't always wrong, and sometimes the old platform truly can't continue. But a big-bang rewrite, where nothing ships until everything is finished, carries the same risk as the waterfall plans of Stage 2: all the learning comes at the end.
The strangler fig
Martin Fowler named a gentler pattern after the strangler fig, a tree that grows around a host tree until it stands on its own. Replace the system one piece at a time, with each piece released and used before the next begins. Here's Pila 1 being replaced by Pila 2:
- Month 1 A router in front Build
| Big-bang rewrite | Strangler fig | |
|---|---|---|
| Value delivered | Only at the end | From month 2 |
| If it’s going badly | Found out late | Found out after the first piece |
| Way back | Hard: the old system has been frozen | Easy: route that piece back |
| Cost | Two systems maintained for the whole project | A router, and some temporary glue code |
Southwest Airlines, December 2022
Over the 2022 winter holidays, a storm disrupted flights across the United States. Most airlines recovered in a few days. Southwest Airlines cancelled nearly 17,000 flights over about ten days, stranding more than two million passengers.
In testimony to the US Senate in February 2023, the airline's chief operating officer described what went wrong technically: as storms broke the planned pairings of crews and planes, so many needed rebuilding that its crew-scheduling software couldn't solve the backlog, and much of the work fell back to people on the phone. The airline's own pilots' and flight attendants' unions had been warning for years that its scheduling systems were outdated.
The US Department of Transportation fined Southwest $140 million in December 2023, about 30 times larger than any penalty it had issued before for breaking consumer protection rules. The consent order was a settlement, not a finding about causes, and it required the airline to compensate passengers and improve how it handles disruptions. Southwest has since invested in upgrading its operational systems.
The lesson for maintenance: the old system worked on ordinary days. Its limits showed only under the rare load nobody had tested, after years in which the cost of replacing it kept being put off.
Spot the trap: “rewrite it in three months”
Six years on, Pila’s code is a tangle and nobody likes working in it. Marco says: “Give me three months and I’ll rewrite the whole thing properly. We’ll switch over in one weekend.”
What happens if…
Check what you learned
Sources
Every claim on this page comes from these sources: the standard, the founding text of a method, a study, or the official record of a project, from the body that publishes it. Industry reports are labelled as such.
- Michael Feathers: Working Effectively with Legacy Code (Prentice Hall, 2004): legacy code as code without tests, and characterisation tests
- Martin Fowler: Strangler Fig Application: replacing a system gradually
- Joel Spolsky: Things You Should Never Do, Part I (2000): the case against rewriting from scratch
- US Department of Transportation: DOT penalizes Southwest Airlines $140 million for 2022 holiday meltdown: the outcome, and what the airline agreed to
- US Department of Transportation: Consent order, Southwest Airlines: the order itself