Refactor or rewrite? How to decide what to do with a messy codebase
Few engineering debates get as heated as "should we just rewrite this?" Rewrites are tempting because the old code is painful, but they carry big risks.
Why rewrites often go wrong:
- The old code contains years of bug fixes and edge-case handling that nobody documented
- You must maintain the old system while building the new one
- The new system takes longer than planned, and the business still needs features
- You may simply rebuild the same problems with new technology
Why incremental refactoring is often safer:
- Each change is small, testable and shippable
- You keep delivering value throughout
- You learn what the code actually does as you go
Patterns that make gradual change possible:
- Strangler fig: route new functionality to a new component and gradually move old features over, until the old system can be retired
- Characterisation tests: write tests capturing current behaviour before changing anything
- Seams: introduce interfaces where you can swap implementations
When a rewrite can be justified:
- The platform or language is truly end-of-life or unsupported
- The system is small enough to rewrite quickly
- Requirements have changed so much that the old design no longer fits
- You can run old and new side by side and compare results
Have you been part of a rewrite that succeeded? What signals would convince you a rewrite is the right call?
Comments · 3
When tests exist, refactoring in small steps feels safe. Without tests, even small changes can feel like a gamble.
We rewrote once and it took three times longer than estimated. Strangler pattern next time: replace one piece at a time.
From a business view, a rewrite means months with zero features shipping. That's a hard sell unless the old system is truly blocking growth.