practice

Refactoring

Changing the internal structure of code without changing its external behaviour, in small verified steps.

maintainabilitydebtchange

The two words that carry the definition are without changing behaviour and small steps. Rewriting a module and changing what it does at the same time is not refactoring; it is a rewrite, and it cannot be verified by the existing tests — which is precisely why it goes wrong.

Architecturally, refactoring is how a system's structure is kept aligned with its current requirements rather than the ones it was designed for. Without it, drift accumulates until the only option left is replacement, which is the most expensive option and the one with the worst success rate.

The precondition is a test suite you trust. Refactoring without one is undirected change, and the usual sequence for legacy code is: add characterisation tests that capture current behaviour first, then refactor.