Refactoring code does not benefit customers in the short term. Then why do refactoring? Is it worth investing time behind it?
Wikipedia defines code refactoring as:
Process of restructuring existing code without changing its external functional behaviour to improve nonfunctional attributes of the software.
In agile software development, all the functionality is not known while starting the development. It starts with some basic requirements, and we keep adding / changing the existing code as we learn more about it. We also change the code later, to fix bugs and edge cases. In this process, as the time progress, the code becomes complex and bloated and gets hard to understand. Many times we write duplicate code without looking at the existing code to the meet the timeline or just because of lack of depth about the entire product.
Customers never bother about the code refactoring as it is not going to benefit them in the short term. And they do not pay any money for refactoring work, so why do refactoring? It is needed for the benefit of developers and long term benefits. Refactoring is generally motivated by “Code Smell”, while debugging some piece of code or adding some additional functionality to the existing code.
Code SmellSo what is “Code Smell”? Code Smell is any symptom in the source code of a program that possibly indicates a deeper problem. Common symptoms which lead to refactoring are duplicate codes, longer methods/functions, too many parameters, non-intuitive variable names, code placement etc. These problems leads to poor readability, longer debugging time and difficult to maintain in the long term.
Refactoring is very important if you are making a product instead of providing custom solution, as it is going to be maintained and improved for a longer period. And It becomes very essential if the code is managed by many other programmers, it helps to the new members to easily dive into the code. Open source software is developed / managed by the community, hence it becomes mandatory to refactor code for the sake of community developers.
There are many benefits of the refactoring. The code becomes more readable and easier to understand. Debugging code is lot more easier and time saving, because of small functions and proper placement. You do not need to worry about duplicate codes, as those are all commonified and in a single place. It is easier to extend the functionality because of reusable functions and design patterns.
Refactoring can lead to new bugs. To prevent that, there has to be complete coverage of automated test cases. Manual testing can be cumbersome for the refactoring and it is not practical to test manually every time after changing the code, it takes a lot of effort and patience. So, before starting refactoring, ensure that you have written enough test cases which will ensure the code functionality. If you planning for releasing a major version and lot of changes are going to be happen, this can be the right time to refactoring the existing code. As it ensures in-depth testing for the software before releasing.
In the last year, I have invested a significant amount of time in refactoring the existing code. I had enjoyed the task thoroughly and already got a huge benefit from it. Unlike the conventional process, sometimes we also do some feature addition if it is very relevant to refactoring. Sometimes adding a functionality helps to remove duplicate codes.
For example, many functionality in the sales and purchase module are almost same, but there were duplicate codes because of different field names and some missing features in purchase module. So, while refactoring code, first thing we did renaming of all fields to ensure the same field names for both the modules. To remove the duplicate code, we also introduced “Inclusive tax” and “Discount Amount” feature in the purchase module. We were developing a major version, which helped to test the refactored codes and our automated test cases also backed up and gave us the right confidence to go ahead. I am sure, it will help other developers to understand the code more easily and to extend the product.