Thursday, June 17, 2021

REFACTORING

 What is Refactoring?

A software is built initially to serve a purpose, or address a need. But there is always a need for enhancement, fixing problems, add a new feature, or enhancing/ modify an existing one.

Periodic changes to the code are made by a plethora of developers of varied experience and coding styles. Hence, over a period of time, the code becomes increasingly complex, adding to its performance and overall maintainability.

To combat this, it's essential to refactor the code periodically to avoid the above-mentioned problems.

Refactoring ensures the quality of the code by restructuring the code without changing the overall behavior.

When to Refactor?

Ideally, refactoring should be a part of the project plan on a periodic basis. It should also be a part of regular programming.
When you have a task of making some fix/ enhancement to the code base, look at the existing code and analyze its readability. If the developer thinks the code is not readable enough to make a straightforward change, take some time to refactor it first. Many times you would already find some code to re-use which is hidden inside some bigger method. Once you refactor them, the job looks simpler.

Then make the required change. 
 
You would notice that it would be a lot faster and less prone to errors/ bugs. 
After a couple of weeks, return to the method and see if it is still readable or needs refactoring again. Chances are, you will find scope for improvement. 
If you visit the same code again after a few weeks, there will still be scope for improvement. 
It's just how the coding works.

Benefits of Refactoring:

1. Improves Understanding and Readability

Translating someones else can be time-consuming and overwhelming. Periodic refactoring improves the design of the software and makes the bigger picture clear. It makes the code easier to understand, and eventually, the software gets cleaned up, for yourself and for other developers who would work on it in the future.

2. Improves development time

Sometimes, it takes several hours just to understand and get the gist of the functionality of a code written by someone else. A well-refactored code is easier to understand and cuts down a lot of development time and reduces regression bugs.

It's easier to write unit test cases for a well-refactored code.

3. Maintainability

Refactoring helps in finding bugs and program faster. It keeps the code clean and organized. A well-refactored code is easier to maintain in the long run.

Automated tools for refactoring

Most IDEs like eclipse/ IntelliJ already have inbuilt plugins that automate the refactoring process. However, it is not mandatory to use them. It is perfectly possible and okay to refactor manually. Just make smaller changes and test frequently to avoid errors. Unit test cases can come in handy too while refactoring. So consider writing as many unit tests to validate your changes.

Conclusion

I have often noticed teams struggling to write unit test cases just to get a good coverage percentage on various quality tools (Sonar, Jacoco, etc.). More often than not, these are mandated by stakeholders to quantify the quality of the source code. But these numbers can be highly misleading, meant only to showcase to the upper management and make pretty ppts.
Periodic refactoring is actually what will get you there. Making enhancements will take a lot less time and it will improve the code quality over a period of time. So it is important, we allocate time to this as part of your regular programming tasks.

REFACTORING

 What is Refactoring? A software is built initially to serve a purpose, or address a need. But there is always a need for enhancement, fixin...