As a developer, sooner or later, hopefully sooner, you will inevitable 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 dirty code to get the job done quickly, forgetting all about it — till one day it comes back to bite you in the ass. 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. In my experience mentoring software engineers, I’ve found it useful to enumerate the various benefits of code refactoring. Hopefully, these will get you to think about it more seriously and perhaps help you convince your manager or colleagues to do the same.
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 never thought about. 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.
Programming isn’t just about writing code that works, but also about writing code which is maintainable by future developers. Trust me, most of the time, 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.
While refactoring you get to look at the coding patterns used from top. While doing so 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.
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.
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. As a programmer you won’t always be working in ideal environment leading to suboptimal solutions. Refactoring your code will compensate for that. It’s very easy to skip or push it for later. 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.