Product and delivery · 21 October 2026 · 4 min read
How do you clean up code that’s too risky to change?
One piece at a time, without changing what the customer sees, adding tests as you go so each step can be proven safe. A full rewrite is rarely the answer.
- Product and delivery
- Refactoring
- Testing
- Architecture
- For CTOs and tech leads
- For engineers
In this article
Every product has a part the team avoids. Small changes there take days, and everyone is a little nervous when they ship. That nervousness has a cost, and it grows with every feature added on top.
How code gets this way
Few products are badly built on purpose. A screen starts small, then gains a feature, a fix, an exception and another feature. Each change makes sense on its own. A year later nobody wants to touch it, because nobody is sure what a change will break. AI coding tools let teams add code faster than ever, so a file can reach that point sooner too.
A real example: a billing page
On a subscription platform for real-time market data, the account billing page had grown into a single file of more than 3,400 lines. Beside it sat a usage tab of more than 1,500 lines. Between them they handled plans and price comparisons, usage and credits, payment details and every kind of subscription change.
Billing is the code you least want to be nervous about, because it touches revenue. Over three days, from the last day of September to 2 October 2026, I restructured it in steps.
Separate the jobs. Plans, usage, payment set-up and subscription changes each moved into their own clearly named parts, so a change to one can’t quietly break another. The usage tab alone became ten smaller files.
Give the tricky behaviour its own home. The page has to stay in step with payments: after checkout it waits until the new subscription is confirmed before showing it, and when billing changes behind the scenes it updates on its own. That behaviour now lives in separate, named pieces instead of being woven through the page.
Share what was duplicated. Payment set-up worked one way on the billing page and another during sign-up. Both now use the same shared code.
Prove it with tests. I added tests for the new pieces as I pulled them out. The billing area went from 14 test files to 21.
When it was done, the main billing file was about 650 lines, and most of the new files are under 500. The customer saw the same billing page. What changed is that the team can work on it with confidence, and find the part they need without scrolling through thousands of lines.
The billing set-up that new customers see when they first subscribe got the same treatment that week. A file of more than 1,800 lines became a set of smaller, focused parts.
One thing didn’t shrink: the total amount of code went up slightly. Clear structure takes a few more lines than one tangled file. That is a fair price, because the aim was code people are willing to change.
The same approach elsewhere
On the same platform I applied the idea in smaller ways:
Two services each had their own copy of the logic for paging through long lists. I replaced both with one shared, tested version.
Hand-written database queries in the admin console were replaced with ones that are checked automatically for mistakes.
Filter bars that had been built separately on about eight admin screens were replaced with one shared set of filters.
Two near-identical dialog boxes were merged into one, and a duplicate card component was retired.
A date and time picker built in-house replaced a third-party one, which removed a dependency the team no longer has to keep up to date.
None of these is dramatic. Together they leave fewer places for bugs to hide, and less code for the next person to learn.
Bugs found on the way through
Restructuring often turns up bugs that were hiding in the tangle. On this platform I found several involving time zones. Date filters could run before the user’s saved time zone had loaded, some invalid dates were accepted, and one filter used a different date field from the one the server used. Each was small, but any of them could have shown a customer the wrong figures.
Signs your code needs this
Small changes take far longer than they should.
Engineers avoid certain parts of the product.
The same bug keeps coming back in slightly different forms.
One file holds thousands of lines and several unrelated jobs.
The code that handles money has few automated tests.
What to avoid
The tempting option is a full rewrite. It usually takes longer than planned, freezes new features while it happens, and brings back old bugs that the original code had quietly fixed. Restructuring in small, tested steps keeps the product working throughout, and each step can ship on its own.
If you’re not sure whether part of your product needs this, ask your engineers which file they least want to open. They usually know straight away.
Related articles
Build it twice: why AI coding agents should throw work away, not patch it forever
When code is cheap to write, the expensive mistake is patching a flawed design forever. Here's how we use AI agents to build fast, keep the lessons, throw the code away and rebuild it properly.
For CTOs and tech leads, Founders
1 November 20268 min read
When mistakes are expensive, a design system is a safety feature
In products where a wrong click is expensive, a design system keeps behaviour consistent. What we got right, and the lesson about numbers we'd learn earlier next time.
For Product and innovation leads, CTOs and tech leads
17 May 20264 min read
Write down why, not just what
Code shows what a system does, not why it does it that way. Short, dated decision records keep the why available for new engineers and AI agents alike.
For CTOs and tech leads, Founders
5 June 20264 min read
Read something that sounds like your problem?
A free 15-minute intro with our CEO. Bring the article, and he’ll tell you which service fits, if any. If a spreadsheet and two rules would do the job, we’ll say so.