Refactoring
How to improve the design of existing code in small, safe, tested steps — without a rewrite. The book that gave the industry its vocabulary for cleaning up after itself.
Two hats
Fowler's first rule is about attention. When you add functionality you are wearing the feature hat: you change behaviour and you add tests. When you refactor you wear the refactoring hat: behaviour stays identical and the tests should all still pass unchanged.
The discipline is never to wear both at once. If a test fails you need to know whether you broke it with new behaviour or with restructuring, and mixing the two makes that question unanswerable.
Why bother
The economic argument is simple. Software is read and modified far more often than it is written, so anything that lowers the cost of the next change compounds. Refactoring is not tidiness for its own sake; it is how a codebase stays cheap to change.
The alternative is what most teams do: keep adding features until the code resists every change, then declare a rewrite. Rewrites are the most expensive possible way to refactor, and they usually lose the accumulated knowledge embedded in the old code.
The safety net
Refactoring without tests is not refactoring, it is hoping. The whole method depends on being able to run a suite after each small step and get a fast, unambiguous answer.
Fowler is direct that if you do not have tests, the first refactoring is to write some — and that the tests do not need to be beautiful, they need to be trustworthy and fast.
The catalogue
The bulk of the book is a reference of named refactorings: Extract Function, Inline Function, Rename Variable, Move Function, Replace Conditional with Polymorphism, Introduce Parameter Object, and dozens more.
Each entry follows the same shape: the situation, the mechanics as numbered steps, and an example. The names matter more than the mechanics, because a shared vocabulary lets a team say "extract that function and move it to the invoice module" instead of describing it for five minutes.
Code smells
Refactorings are the cure; smells are the symptoms. Fowler catalogues the recurring shapes that indicate a design problem: a function that has grown to do three things, a class that reaches into another's data, a parameter list that keeps growing, a switch that appears in five places.
The point of naming smells is that they turn a vague sense that something is wrong into a specific diagnosis with a specific remedy.
Preparatory refactoring
The most practically valuable idea is that you refactor before you add the feature. If a change looks hard, the reason is usually that the code is shaped wrong for it.
Restructure first so that the feature becomes obvious, then add it. The work feels slower for an hour and is faster for the rest of the project.
What it is not
Refactoring is not a phase you schedule after delivery, and it is not a licence to restructure everything you dislike. It is a continuous, small, local practice driven by the next change you need to make.
Fowler is also honest that refactoring has a cost, and that in some codebases — the ones with no tests and no ownership — the honest answer is to stop and build the safety net first.
The bottom line
Change structure in small tested steps, one hat at a time, guided by named smells and named remedies. The goal is not beautiful code. It is code that stays cheap to change, which is the only thing that keeps a project alive.
🔑 Key actionable ideas
- Refactoring means changing the internal structure of code without changing what it does. Behaviour is the contract; everything else is negotiable.
- Small steps with tests after each one are what make it safe. A large refactor is just a rewrite wearing a disguise.
- Never add a feature and refactor at the same time. Wear one hat at a time, or you will not know which change broke something.
- Code smells are the diagnostic vocabulary: duplicated logic, long functions, long parameter lists, feature envy, primitive obsession.
- Refactor before adding the feature, not after. Making the change easy first usually makes the easy change trivial.
Who is this book for?
Working developers and technical leads responsible for code that other people have to modify, and anyone who has watched a codebase become unmaintainable.
Final verdict
The definitive book on the subject, and the source of the vocabulary the industry actually uses. Dense as a straight read because of the catalogue, but indispensable as a reference.