Andrew Nater

Is It Time to Refactor?

It can be difficult to determine the right time to refactor your code. There’s a lot to consider. Is this refactor going to make life easier? How much effort will it require? Is it going to affect critical functionality? The answers to these questions will help you determine whether or not to refactor your code.

Is this refactor going to make life easier?

Say you have some code that you only deal with every now and then, but its cumbersome whenever you do have to work with it. In fact, it’s a nightmare. There are no comments, things are poorly named, and it’s hard to follow what’s happening in the code.

By the sound of things, a refactor may make life easier when working with this code. But what about life after refactoring this code? Will it solve all the problems you have with it today. In general, if refactoring your code will save you time, effort, and headaches then its worth it.

How much effort will the refactor require?

In essence, how long will it take? How much time thinking, planning, implementing, and testing will be spent refactoring? The answer to this question will probably surprise you since you may not work with a piece of code very much in the future, especially if its working code.

Sometimes our inclination to refactor is about wanting to modernize our approach with new knowledge in mind. This is okay if the modern approach will save us effort in the future and requires minimal effort to accomplish. Learn to balance future and present effort.

Will the refactor affect critical functionality?

It is risky to refactor code that is working and is critical functionality. You risk introducing new bugs and ruining the user experience. New bugs will make life harder and introduce additional effort. Diminishing the user experience would negatively impact users, again making life harder and adding effort. This is a very important point to consider in your plans to refactor.

If you risk negatively affecting working code, you may rethink your plans to refactor. The risk could easily outweigh the benefits the refactor brings to your code base. Inelegant working code is better than elegant buggy code.