Refactoring or rewrite?
Refactor in small steps by default; most rewrites take longer than planned and lose behaviour nobody wrote down. Rewrite only behind a strangler fig, piece by piece, with old and new running side by side. How to decide, and how to do either without a big-bang cutover.
The verdict, which the cases below earn: refactor by default; when a rewrite is needed, do it incrementally.
- Refactor (change structure in small, safe steps while behaviour stays the same) when the system works but is hard to change.
- Rewrite a component when its platform is a dead end, its design can’t meet a new requirement, or the team can no longer maintain it. Even then, replace it piece by piece behind a strangler fig: new code takes over one route or capability at a time while the old system keeps running.
- Never do a big-bang rewrite that ships only when it’s “done”. The old system keeps changing, the new one chases it, and undocumented behaviour gets lost.
Case file
The case The module nobody wants to touch, the service that “needs a rewrite” Suspects Ugly, old code, the wrong language Culprit Changing a working system without a way back The fix Refactor by default, characterisation tests first, strangler fig for rewrites
The case #
Every team has one: the module nobody wants to touch, or the service everyone agrees “needs a rewrite”. The tempting suspect is the code itself, ugly and old. But the real question is how to change a working system without ever breaking it, and there’s a real pleasure in watching that done well. The ideas below make it possible: they let you be brave with code because you’re never far from a way back.
The three suspects at a glance #
The three approaches differ most in when they deliver and how they fail:
| Incremental refactoring | Strangler-fig rewrite | Big-bang rewrite | |
|---|---|---|---|
| Delivers value | Continuously | Per migrated piece | Only at the end |
| Risk | Low per step | Moderate, contained per piece | High, all at once |
| Rollback | Revert a commit | Route traffic back to the old path | Often impossible |
| Needs | Tests around the code you change | A routing layer, comparison of old and new outputs | Faith |
| Hidden behaviour | Preserved | Discovered piece by piece | Lost, then found in production |
Use case 1: migrating a legacy service to a new stack #
The scene: a service’s platform is a dead end for the team: hiring, libraries, or the team’s skills point elsewhere, and new requirements (real-time publishing, personalised feeds) need a different design.
At Jio News, I led the backend re-architecture of a service with about 5 million active users across more than 12 languages, moving legacy services from Go to Node.js (TypeScript) within two months, together with new services for real-time publishing and personalised feeds at sub-200 ms latency under peak load. A move like that only works incrementally. Martin Fowler’s strangler fig metaphor fits beautifully: the new system grows around the old one until the old one can be switched off:
- Put a routing layer in front (a gateway or reverse proxy) so each route can be served by the old or the new code.
- Migrate one capability at a time, starting with something low-risk but real.
- Compare outputs for the same requests (shadow traffic or sampled comparison) before switching each route.
- Shift traffic in steps you can reverse, and retire old code only after the new path has carried real peak traffic.
The zero-downtime migrations case covers the same sequence for databases.
Use case 2: a module with no tests #
The scene: a core module nobody wants to touch. It works, mostly, but every change breaks something.
What I’d do: refactor, starting with tests that describe what it does today. Think of it as taking witness statements before you move anything.
- Characterisation tests (Michael Feathers’ idea from Working Effectively with Legacy Code; the test-first case covers where they fit): record what the module currently returns for a wide range of real inputs (sampled from production, anonymised) and assert exactly that, bugs included.
- Refactor in small steps: extract a function, rename, separate I/O from logic. Run the tests after each step.
- Fix bugs separately, each as its own change with its own test, so a behaviour change is always deliberate.
After a few weeks, the module is usually understandable, and the case for a rewrite often collapses. I find that one of the most satisfying reveals in software: the scary module turns out to be ordinary code.
Use case 3: a framework or platform upgrade #
The scene: a business system built on a framework, with custom apps on top, is several major versions behind. At CapGrid, I set up our ERPNext / Frappe ERP and later upgraded it from v14 to v16.
What I’d do: treat the upgrade as a series of small refactors, not a rewrite.
- Inventory the customisations first: custom apps, scripts, hooks, changed standard behaviour. Each is a risk to check.
- Upgrade one major version at a time where the framework requires it, on a copy of production data.
- Test the business flows, not only that it starts: a document from creation to completion, the reports people rely on, the integrations.
- Rehearse the cutover on a production-sized copy, with a timed run-book and a tested way back (a backup you’ve restored).
Diagram source
flowchart TD
Q1{"Platform dead end, or a requirement the design can't meet?"}
Q1 -->|no| RF["Refactor in small steps"]
RF --> CT["Characterisation tests first"]
Q1 -->|yes| SF["Strangler fig rewrite"]
SF --> RL["Routing layer in front"]
RL --> ONE["Migrate one capability, compare outputs"]
ONE --> SH["Shift traffic in reversible steps"]
SH --> ONE
Q1 -.->|never| BB["Big-bang rewrite"]
When a rewrite really is the answer #
- The platform is unsupported or insecure, and upgrading is impossible.
- A new requirement contradicts the core design (single-tenant to multi-tenant, batch to real-time).
- The code has become so tangled that small changes take weeks, and refactoring has been tried.
- The component is small enough to rewrite and replace in weeks, not quarters.
“The code is ugly” and “we’d use a different language today” are red herrings; they aren’t reasons on their own.
Remember this
- Refactor by default, rewrite piece by piece, never big-bang.
- Take witness statements first: characterisation tests, bugs included.
- Keep the old path until the new one has carried real peak traffic.
What I’d do on Monday #
- Refactor continuously, as part of normal work, with tests first.
- Characterisation tests before touching legacy code.
- Strangler fig for rewrites: a routing layer, one piece at a time, outputs compared, traffic shifted gradually.
- Keep the old path until the new one has carried real peak traffic.
- Separate behaviour changes from structural changes, so every behaviour change is deliberate and reviewable.
All of this rewards patience: small steps, a way back at every point, and the old system kept running until the new one has carried real traffic. It’s a quiet solution, and that’s exactly what I admire about it.
Comments
No comments yet.
Sign in to comment. We'll email you a link; no password needed.