Refactoring is restructuring existing code. Refactoring is carried out to remove code smells. Refactoring does NOT fix bugs. Refactoring is done when there are robust unit test cases to test the code. These test cases are executed before and after refactoring the code and test case results must be the same before and after. This ensures there are no bugs introduced due to refactoring.
As a practical example, a software was supporting a protocol and requirements got changed and we wanted to support a second protocol, refactoring was done to extract the source code that stands common for both the protocols and protocol specific code was separated out and common interface was provided. The point is that sometimes refactoring becomes mandatory to incorporate new software requirements. Refactoring is also applied just to improve the code readability, better maintenance and extendability purposes.
1. Some refactors are controversial
2. Some are arguably not improving code quality
3. Refactoring can in fact be counter productive when applied blindly
4. Apply in combination and make sure refactoring is applied in productive manner.
1. Extract Method: If your method is too long and is performing multiple tasks, the code snippet that is doing a unique task that main method name does imply, that code snippet is extracted into a method.
2. Replace temp with query: This is about assigning a query result into a variable and then using that variable, instead the query result can directly be used. This is controversial anyway.
3. Replace type code with state/strategy: You have a type code that affects the behavior of a class, but you cannot use subclassing.
4. Replace conditional with polymorphism : You have a conditional that chooses different behavior depending on the type of an object.
5. Form template method: You have two methods in subclasses that perform similar steps in the same order, yet the steps are different.
6. Replace Magic Numbers with symbolic constant: You have a literal number with a particular meaning
As a practical example, a software was supporting a protocol and requirements got changed and we wanted to support a second protocol, refactoring was done to extract the source code that stands common for both the protocols and protocol specific code was separated out and common interface was provided. The point is that sometimes refactoring becomes mandatory to incorporate new software requirements. Refactoring is also applied just to improve the code readability, better maintenance and extendability purposes.
1. Some refactors are controversial
2. Some are arguably not improving code quality
3. Refactoring can in fact be counter productive when applied blindly
4. Apply in combination and make sure refactoring is applied in productive manner.
Refactoring Methods
1. Extract Method: If your method is too long and is performing multiple tasks, the code snippet that is doing a unique task that main method name does imply, that code snippet is extracted into a method.
2. Replace temp with query: This is about assigning a query result into a variable and then using that variable, instead the query result can directly be used. This is controversial anyway.
3. Replace type code with state/strategy: You have a type code that affects the behavior of a class, but you cannot use subclassing.
4. Replace conditional with polymorphism : You have a conditional that chooses different behavior depending on the type of an object.
5. Form template method: You have two methods in subclasses that perform similar steps in the same order, yet the steps are different.
6. Replace Magic Numbers with symbolic constant: You have a literal number with a particular meaning
No comments:
Post a Comment