We usually have no time to refactor, the client wants us to work on bugs fixing or new features. S/he pays for it, so we’re doing what s/he wants. Time goes and we keep adding new features, finding new bugs, debugging, fixing, adding features, debugging and so on. The client is happy, s/he sees we always work on the actual ticket, which improves the visible part of the application, s/he is glad we’re not wasting time on refactoring, s/he thinks it’s pointless to change the things that work. The truth is that thanks to refactoring, we save time.
What is refactoring?
In case you’re not familiar with refactoring, let me paste here two definitions from the “Refactoring. The Ruby Edition” book.
Refactoring (noun): A change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior.
Refactor (verb): To restructure software by applying a series of refactorings without changing its observable behavior.
First of all: tests
The most important thing when you refactor is the test coverage. You just can’t refactor the code without tests. Before even thinking about refactoring, make sure the piece of code you’re about to refactor is well tested. Otherwise, you’ll more likely introduce new bugs rather than improve the code.
Why to refactor?
There are many reasons of why refactoring becomes so important in the world of programming. I think the most important are these ones:
Refactoring makes code easier to understand
Refactoring should make the code easier to understand, I think otherwise it’s not refactoring. Let me quote Martin Fowler here:
I make a point of trying to put everything I should remember in my code so I don’t have to remember it.
By making the code easier to understand you make it easier to work with.
Refactoring helps you to develop yourself
By refactoring you improve your skills. Not only by reading other developers' code, but also by making the code you wrote a year ago better.
Refactoring improves overall app design
As a programmer, you learn new things over time. Refactoring usually leads to overall app design improvements, the solutions you (or your colleague) implemented months ago, can be not so good today, as you know more.
Refactoring helps you understand the project better
When you refactor, you simply have to understand the code you’re (re)writing. By refactoring you’re getting to know the project you work on better.
Refactoring helps you find bugs
By getting more and more familiar with the codebase, it’s easier to find the bugs.
Refactoring allows you to program faster
All the facts above come to this. When you refactor, every time you learn more about the project and its code is easier to understand for you and your colleagues. Thanks to that you are extending/improving the existing codebase faster.
If your client tells you that refactoring is a waste of time, you can try to explain that it’s a time investment. By refactoring today, we’ll fix bugs and code faster tomorrow.