Maintenance and the end of life · lesson 4 of 7
Ideas in this lesson
ALegacy codecode without tests (Michael Feathers)
BCharacterisation testrecords what the code does now
CStrangler figgrow the new system around the old, piece by piece

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.

  1. Call the code with some input.
  2. Write an assertion you know is wrong, such as assert result == "?".
  3. Run it. The failure tells you the actual result. Put that in the assertion.
  4. 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.

A characterisation test shows the old code returns 61 numbers a day, not 60. What do you do?

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:

  1. Month 1 A router in front Build
1 of 5 Month 1. Every request now goes through a small router that sends it to old Pila. Nothing else changes. Patients notice nothing.
Big-bang rewrite or strangler fig
Big-bang rewriteStrangler fig
Value deliveredOnly at the endFrom month 2
If it’s going badlyFound out lateFound out after the first piece
Way backHard: the old system has been frozenEasy: route that piece back
CostTwo systems maintained for the whole projectA 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”

Spot the trapMarco

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

By Michael Feathers’ definition, what is legacy code?
What does a characterisation test record?
What is the main advantage of the strangler fig pattern?

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.

All sources for Simple Software Development