Refactoring vs Rewriting

Hansel Dunlop
TSEngineering
Published in
2 min readOct 14, 2018

People often use these terms interchangeably. Which scares me. Because I have a lot of anxiety around the process of rewriting.

While I have absolutely none around the activity of refactoring.

Let me explain that.

Rewriting a function, class, module, project or whole application is something inherently risky. It’s possible. If you have excellent test coverage. A comprehensive suite that covers all the expected behaviour and captures the edge cases that your software has discovered by interacting with the real world.

I’m not saying that it’s never the correct choice. Just that you need to understand the risk and uncertainty that you are inviting into your life by doing it.

Refactoring on the other hand is a maintenance activity designed to prevent you from ever having to do a rewrite. And at its core is about making the code you have as easy as possible to understand.

It’s an activity that every person on a team can do continuously. It’s not an activity that is limited by seniority.

Refactoring is also a fantastic way to figure out if your tests are ‘good’. Because good tests, that mock at the appropriate interface, allow you to change your code with minimal changes to the tests. It’s a great way to learn this. A great way to gain an intuitive understanding of where the appropriate level of mocking is.

Let the right one in. Refactor more, rewrite hardly ever.

--

--