Can't find what you are looking for ?

Thursday, July 9, 2009

Refactoring - Code improvement without changing behavior

Refactoring is the process of changing a software system in such a way that it does not alter the external behaviour of the code/design, yet improves its internal structure. When the software is refactored, the existing design is examined for redundancy, unused design elements, inefficient or unnecessary algorithms, poorly constructed or inappropriate data structures, or any other design failure; once this inspection is done, then the results are analysed for getting a more improved design.
If you were to ask for a simple explanation, then code refactoring means that you 'clean up' the code. Refactoring is not meant to be used for changes where you added new functionality, or fixed bugs, or something similar. It is in principle used for analysing code to remove dead code, to make the code more understandable, and modify the internal structure and design (maybe to make the code more elegant, as well as to follow some standard code processes and layouts).
Why do refactoring ? The process intends to make code easier to read and comprehend, and also to be able to make modifications to it. Given the amount of time that code remains in-use, and with the people handling the code not remaining constant, it is important that the code be easy to understand and maintain. Sometimes people take on the task of improving the code in order to fix defects or solve other design issues, but that is not refactoring.
A common method used within refactoring is to change the definition of variables, such as when a non-meaning variable such as 'j' is instead changed to actually be closer to the real use of the variable, say 'ItemWeight'. This makes it easier for somebody else to quickly read the code and comprehend. This however, has its own set of complications since the same variable may be used in many different parts of the application, and the person making the modification may need to study each usage before making the change.

No comments: