Writing

Refactoring is as important as building

"My PM never gave us time for technical debt" is one of the most common complaints in our industry. Most of the time, the problem is the pitch. How I justify refactoring in terms a product owner can weigh, how I plan for it, and why I don't let AI decide what to refactor.

Published
Reading time
10 min read
A person repairing a computer server under a lamp
Photo: By Yuriy Vertikov on Unsplash

There's a sentence I've heard from developers in almost every team I've talked to: "My PM never had time for us to address the technical debt." It usually comes with a sigh, and with the assumption that product people simply don't care about code quality.

I recently came across a LinkedIn post that answered it bluntly: stop saying "technical debt", and explain what the refactoring will do for the product instead. How much development time will you save once the data layer is cleaned up? How much faster will the API respond after the framework upgrade? How many production errors will tests in the pipeline catch? How much money will you save by moving off that huge VM?

I agree with most of it. When a refactoring request gets rejected, the reason is usually the way it was asked for, much more often than the person deciding.

#What a PM hears

From the inside, technical debt is concrete. You know the module where every change breaks two other things, the query that takes four seconds, the deploy that only one person knows how to run. You feel it every day.

From the outside, "we need two weeks for technical debt" sounds like this: we'd like to stop shipping for two weeks, the product won't look any different at the end, and we can't tell you what you get in return. Put yourself in the shoes of someone who answers to customers, sales and a roadmap. That request competes with a feature a customer is waiting for, and the feature has a clear return. Of course it loses.

The PM isn't against refactoring. They just can't weigh a cost against a benefit nobody described.

#Keep the term, attach the numbers

I don't think the phrase itself is the problem. The debt metaphor is actually a good one, and it's the part people forget: debt has interest. You pay a little every time you touch the code, and the payments get bigger the longer you wait.

So keep saying "technical debt" if you like, but always say what the interest is. Compare these two requests:

We have a lot of technical debt in the data layer and need a sprint to clean it up.

Every feature that touches orders takes us about three extra days, because the data access is duplicated in five places and each change has to be made and tested in all of them. We had four of those features last quarter and the roadmap has six more. Consolidating it takes about a week, and I expect it to cut those features down by two days each.

The first one asks for trust. The second one is a trade the PM can actually evaluate, and in that case it pays for itself before the end of the quarter.

The questions from that LinkedIn post are a good template, because each one maps a technical problem to something the business already tracks:

  • Development time. How long do features in this area take now, and how long would they take after?
  • Performance. What's the response time today, what will it be after the upgrade, and does it affect conversion, timeouts or customer complaints?
  • Reliability. How many incidents or bug reports came from this area last month, and how many would tests or the refactor have prevented?
  • Cost. What does the current infrastructure cost per month, and what would the new setup cost?

You won't always have precise answers, and that's fine. A rough estimate with an honest margin is far more convincing than no number at all.

#Estimate the cost of not refactoring

The most useful number is usually the cost of leaving things as they are, because that's the cost the business is already paying without seeing it.

You don't need special tooling for this. Look at your issue tracker and your git history for the last few months, and pick the area you want to refactor:

Last quarterExpected after refactor
Features touching orders46 planned
Average time per feature8 days5 days
Bugs reported in that area11fewer, with tests on the new layer
Refactoring cost~5 days

A table like this does two things. It turns a vague feeling into something you can discuss, and it forces you to check whether the refactor is worth it at all. Sometimes the numbers say it isn't, because the messy area is rarely touched and works fine. That's a useful answer too, and it saves you from spending a week on code that didn't need it.

#When the benefit is hard to measure

Some refactors don't map to a clean number. Better naming, clearer module boundaries and removing dead code mostly make the code easier to understand, and "easier to understand" doesn't show up on a dashboard.

For those, I use proxy metrics:

  • Lead time for changes in that area, from starting a ticket to deploying it.
  • Bug rate in the files you want to touch, compared to the rest of the codebase.
  • Onboarding time, how long it takes a new developer to make their first meaningful change there.
  • Estimate accuracy, how often tickets in that area take longer than planned.

None of these is perfect, but together they describe what "this code is hard to work with" actually costs. If even the proxies show nothing, I take that as a hint that the refactor might be more about my taste than about the product.

#How I've handled it as a one-man team

For most of my career, I've been the only developer on the projects I worked on. There was no tech lead to argue for refactoring time, so I had to do it myself.

What worked for me was planning for it from the start. I never put exact dates on refactoring, because you can't predict when an area will get messy. But when I planned a project, I kept room for it, a bit like a buffer that I knew would be spent on something. Then, when a part of the codebase started to slow me down, when it got harder to extend for the next features, or when performance issues started showing up, I'd pause feature work and refactor.

I didn't ask for permission each time. I informed the stakeholders that I was taking a refactoring break, explained which problem it solved, and how it would help the project keep up with what the business needed. Because that time was already in the plan, it never came as a surprise, and nobody felt like they were losing something they'd been promised.

I think that's the part that transfers to bigger teams. If refactoring only shows up as an exceptional request, it will always look like a delay. If it's part of how you plan, it's just part of the work.

#Make it easy to say yes

A few habits make refactoring requests much easier to approve.

Tie it to the roadmap. The best time to refactor an area is right before you build something in it. "Before we build multi-currency pricing, I want to consolidate how prices are calculated, otherwise we'll have to add currency handling in four places" is a request that's attached to a feature the PM already wants. It's the same work, but it now has a deadline and a reason.

Keep it small. A two-day refactor is easy to approve, and if it turns out to be wrong, you've lost two days. Three months on a new architecture is a bet the business has to take on faith. Most messes can be cleaned up in slices: move one module to the new data layer, ship it, see if it helps, then do the next.

Report back. After the refactor, measure the same things you estimated. If the next orders feature took four days instead of eight, say so. If the API is 300 ms faster, show the graph. Each refactor that delivers what you promised makes the next one easier to approve, because you're no longer asking for trust you haven't earned. And if it didn't deliver, saying so honestly earns you even more of it.

#PMs have a part in this too

None of this means product people are off the hook. A PM who never leaves room for maintenance is making a decision with consequences, whether they see them or not: features slow down, incidents go up, and eventually the team spends more time fighting the code than building with it. Code doesn't stay healthy on its own.

A good PM budgets for that, the same way they'd budget for support or infrastructure. But they can only do it if engineers give them something to budget with. That responsibility is shared, and the engineering half of it is the one we control.

#About rewrites

Sometimes a refactor isn't enough, and the honest answer is a complete rewrite. I don't think rewrites are always wrong. The framework is no longer maintained, the architecture can't support what the business now needs, the security issues can't be fixed in place: those are real reasons.

But a rewrite should be the most strongly justified decision you make. You lose years of bug fixes and edge cases that live in the old code without being written down anywhere, and you spend months building something that, on day one, does what the old system already did. The problems you'd have by not rewriting need to be serious, not just inconvenient. "The code is ugly" or "nobody uses this framework anymore" isn't enough. "We can't ship the features on the roadmap with this, and here's why" might be.

#AI made refactoring cheap, not wise

With coding agents, refactoring has never been cheaper to execute. You can ask a model to migrate a codebase to a new pattern, rename half your modules or move to the framework everyone's talking about this month, and it'll do most of it in an afternoon.

That's exactly why I'm careful with it. When change is that easy, it's tempting to refactor for a trend or for taste, because the cost seems to be gone. It isn't. Every change still has to be reviewed, tested and understood by whoever maintains the code next. A cheap refactor that nobody needed is still a refactor nobody needed.

My opinion is that the thinking part of refactoring shouldn't be handed to AI. Deciding what to refactor, why, and what the code should look like afterwards comes from knowing the product, the business and the history of the codebase. That's human insight, and it's where the value of a refactor actually comes from. I'm fine with a model doing some of the typing once I know exactly what I want. I don't want it deciding that my code needs to change, or how.

#Conclusion

Refactoring is part of building software, as much as writing new features. But it only gets the time it deserves when the people deciding can see what it buys them. So when your next refactoring request comes up, don't just say "technical debt". Say what the interest is, what paying it off will save, and how you'll prove it afterwards.

  • Refactoring
  • Technical Debt
  • Thoughts
  • Productivity