Why You Must Refactor Your Code

Simpl
Simpl - Under The Hood
4 min readJun 1, 2016

If you’re a developer, sooner or later you will inevitably hear something along the lines of:

“Don’t waste time writing great code. Refactor your code later. Why are you wasting time figuring out this new design pattern. The client/user doesn’t care about the quality of your code. Just get the feature built.There is a deadline tomorrow. That’s all that matters.”

In context, there’s a possibility that the people telling you this are correct. You will fight against it, but over time it will start making sense to you as well. You’ll make your peace with it and soon it’s a downward slope of writing bad code to get the job done quickly, forgetting all about it — till one day it comes back to bite you. The only way out of this mess is to refactor your code.

Refactoring won’t make your code run faster or add new features to your product but it is still important to do it. At Simpl, we constantly encourage software engineers to refactor their code. In this article, we will enumerate the various benefits of code refactoring. We hope you find this useful in convincing your engineers to refactor their code as well.

Structure

Whenever you code to achieve short-term goals or without comprehending the complete problem, the code structure takes a hit. This loss of structure has long-term cumulative impact on the future work that will be done. Reading, understanding and managing a code-base which does not have proper structure is far more difficult and time consuming. It becomes harder to predict the outcomes of any change you make to existing code. With proper structuring, your code becomes intuitive and predictable for other developers.

Often, you start with a small product and design your application accordingly. Over time, your application becomes larger with many new features which you didn’t account for earlier. Some of these features don’t fit well in your current structure. Refactoring gives you the opportunity to rethink the structure and modify it to better incorporate the new features/changes.

Understandability

Programming isn’t just about writing code that works, but also about writing code which is maintainable by future developers. And more often than not, this future developer will be you! When you are writing code to get the work done you are not thinking about this future developer. While refactoring, you get to see if the current code is enough to express what it does, if not then you can modify it to do so. Remember, software creation is a collaborative process. Most great software is built by teams, over years. Refactor to make your code understandable and you’ll be building better software.

The DRY Principle

Poorly structured code typically takes more lines of code to do the same thing as a lot of code is duplicated in several places. While making changes you will have to make similar modifications in multiple places or sometimes you will change in one place and forget to do it in others. By removing the duplicate code you follow the DRY principle of not repeating yourself.

Learning

While refactoring you get a bird’s eye view of the coding patterns used. You will see the pros and cons of choices you made while developing and solving a problem. Which will in turn help you in making better choices in future while solving similar problems. You will also notice commonly occurring coding problems which can be solved using existing design patterns or a new design pattern of your own. All of this will help you grow as a programmer and take you to next level.

Efficiency

This is what matters the most to your manager and clients. All of the points mentioned above will contribute in reducing development time on an existing code. Every programmer knows the difference between working on a well structured code and a poorly managed code (even if it’s written by you). It’s nearly impossible to build something robust and stable on top of a shaky base.

Pride

As a programmer I think it’s very important that at the end of the day you are proud of the code you write. Just because non-programmers can’t see the beauty of your code doesn’t mean that you shouldn’t care about your code. Refactoring your code will make it beautiful and elegant which in turn will motivate you to write better code with every passing day.

Refactoring is not a silver bullet to solve all your problems, it’s a preventive measure. It’s easy to skip refactoring altogether or procrastinate. You need to make it an integral part of your coding cycle and follow it religiously. It requires a lot of discipline and efforts to inculcate it but in the end you will thank yourself for doing it.

This article was written by Akshay Shinde, a product manager (but engineer at heart) at Simpl.

--

--