When we work on a project as a developer our main focus is to write code and complete the requirements of the modules given to us, once we are done we just call it a day, when the other developers try to refer the module we develop they are clueless most of the time and the worst part we ourselves forget what we did and what a particular line of code is supposed to do.
In Agile software development we follow practices like Test Driven Development(TDD), Refactoring to address this kind of confusions, Refactoring is a disciplined technique for restructuring an existing body of code, meaning altering its internal structure without changing its external behavior. When we try to implement a functionality for the first time we are focused to achieve the result, that’s totally fine but if we spend a little more time following the guidelines of what TDD and Refactoring says we can save a lot of time in the future and we will be thinking about adding features instead of working on the bugs.
TDD says that we should first write aUnit Test before implementing the functionality, this ensures that the method which we are about to implement returns the expected result, the more tests we write to cover all the conditions the method will run under the more stable the functionality. Another main benefit of this step is that in future when someone else modifies/enhances this method they wont be breaking the functionality, this is very important when we work as a team and the project undergoes change in a rapid pace.
When we write this test first it is going to break obviously since there is no method to back up the test but we should not consider this step as a trivial one because this test drives a lot of attributes about the method we are about to write, right from the name to the signature and the return type. The failed test gives us the Red alarm and there our goal should be turning that into Green, that where we go crazy and write lines and lines of code and implement the functionality and pass make the test Pass.
Now comes the important step,Refactoring the code, we have to covert the code we wrote from a complex block of language syntax to a simpler form, reading the code should be like reading a story, the time we allocate is for this process is definitely well spent, it saves lot of our time in the future. This is not the only time we should consider refactoring the code, generally when there is a feeling that we are complicating things we should consider Refactoring the code, these signals are called “code smells”, code smells indicates when to Refactor, some of the common code smells are
One question that is asked often about Refactoring is that whether it will improve performance or not, Refactoring is not a step which considers to improve performance but refactoring helps us to do that indirectly, it improves software quality, it makes maintenance easy so it makes performance optimizations easier and ensures that the we are not breaking the system when we do performance optimizations.
Before:
After: