What is code refactoring?
Changing code's internal structure without altering its observable behavior, one small step at a time, under test coverage.
Code refactoring is the practice of changing a program's internal structure without altering its observable behavior: same input, same output, but code that is clearer to read and cheaper to change later. The definition comes from Martin Fowler, who in the book Refactoring (1999, second edition 2018) describes it as a series of small transformations, each nearly imperceptible on its own, that add up to a meaningful change. Fixing a bug falls outside the definition: if behavior changes, it is a functional change. Rewriting from scratch is something else again: refactoring works on the existing code, one verifiable step at a time, keeping the system working and releasable throughout. The goal Fowler states is making code easier to understand and cheaper to change, not making it "more elegant" in the abstract: every transformation should be justifiable by a future change that becomes simpler because of it.
See also: Extreme ProgrammingA software development method from the 1990s: engineering practices such as tests written before the code and pair programming, meant to make change cheap.
Why a test safety net has to come first
Refactoring only works when there is a fast, reliable way to notice that behavior changed when it should not have: without that net, every transformation is a silent bet. That is why Fowler ties refactoring closely to automated tests, especially the wide base of the test automation pyramid: the fast, cheap unit tests are the ones run after every small change to confirm nothing broke. Refactoring without tests, in practice, looks more like a rewrite done in pieces, with all the risk of a rewrite and none of refactoring's guarantees.
How technical debt actually gets paid off
Refactoring is the mechanism by which technical debt is actually repaid, not just named in a meeting: it is the concrete work of retiring the principal, while living with the shortcuts without ever touching them amounts to paying interest forever. On a system that is losing ground against its own purpose, targeted refactoring is often the cheaper alternative to incremental replacement, reserved for cases where the codebase is compromised past the point where small steps are enough; on a legacy system the first question is always which of the two applies, because picking the wrong one costs months.
AI changes the speed, not the principle
Code assistants can propose and apply refactoring transformations far faster than before, but the principle stays the same: no transformation lands without going through the tests, and generated code still has to be read and understood by whoever maintains it, not just run. One of the best uses of AI here is the reverse of refactoring: spotting the signals of unreviewed generated code that will become the next debt to repay.
A concrete example
A logistics company has a rate calculation module nobody wants to touch anymore: a single function hundreds of lines long, with per-customer exceptions stacked one on top of another. Before adding a new price list, the team writes tests that pin the current rates on a sample of real shipments, then extracts one rule at a time into named functions, rerunning the tests after every step. The rates stay identical, and the new price list becomes one more function instead of another exception.
Why it matters for decision makers
For someone who does not write code, refactoring should be budgeted as recurring maintenance, not as a one-off project to approve and close: in the practice Fowler describes it is continuous and woven into feature work, with no separate phase blocking delivery. The signal that it is needed can be measured: estimates that double against historical baselines, releases that get rarer, a module the team avoids touching.
Related terms
- Extreme ProgrammingA software development method from the 1990s: engineering practices such as tests written before the code and pair programming, meant to make change cheap.
- Test automation pyramidThe test automation pyramid guides the mix of automated checks, favoring narrowly scoped tests and pairing them with checks on integrations and workflows.
- Strangler fig patternThe strangler fig pattern gradually replaces an existing system by moving functionality and requests to a new one while the old system remains operational.
- Legacy systemAn application still critical but hard to change: obsolete stack, scarce skills, no support from the original vendor.
- AI code smellRecurring defects in LLM and agent written code: it passes the demo, but maintenance costs more than expected.
If it reminds you of a system nobody dares touch anymore, that is what I put back in order.
See Software Rescue