What it is
Reworking code without changing what it does: clearer names, less repetition, simpler structure. Users see no difference, but further development becomes faster and cheaper.
How we use it
We refactor while working on a task, backed by automated tests that confirm everything still works as before.
Where refactoring helps a business
- A simple change turns out to be surprisingly expensive: the same rule is repeated in five places and has to be changed in all of them.
- The project came from another developer, and the code is hard to follow.
- A big feature is coming, and the part of the code it will plug into needs tidying up first.
- Technical debt has piled up, and it is time to pay it off.
How we use it
- Along with the task. We refactor the part of the code we are working on right now, not as a separate "month of rework" with nothing to show for it.
- Backed by tests. Automated tests confirm that everything works as before after the rework: leads arrive, totals add up, the bot replies.
- In pieces. Instead of a rewrite from scratch we change the code one part at a time, and every release can be rolled back. This is also how we handle legacy code.
Common problems
- Refactoring without tests. Then it is not rework "without changing behaviour" but a blind rewrite: customers will find out what broke.
- Rework and a new feature in one change. If something goes wrong, there is no telling what caused it: the new feature or the reshuffled old code.
- Endless polishing. Tidying up code that nobody will ever touch again is money spent for nothing.
When you do not need it
Code that is about to be switched off or replaced, and code that has run for years without changes, does not need refactoring. Rework pays off where you will keep coming back with new features.