Professional Documents
Culture Documents
CS314 Software
Engineering
Refactoring changes the internal structure of software to make it easier to
understand and cheaper to modify without changing its observable behavior.
What is it? When do you do it?
• Disciplined way to clean up code • Adding a function or method
• Improves the design of software • Fixing a bug
• Makes software easier to understand • Code review
• Minimizes the chances of introducing
bugs
• Helps you find bugs
• Helps you program faster.
Use Extract Method Use Extract Method Use Extract Class and Use Introduce
and call the new to create new Extract Subclass Parameter Object or
method from both methods that obey methods to deal with Replace Parameter
places. the Single variables, Extract with Method to
Responsibility Method to deal with reduce the number of
Principle. duplication. parameters.
1) Create a new class to represent the group of parameters you are replacing. Make the
class immutable. Compile.
2) Retain the old method signature with a new body that maps the parameters to the new
objects.
3) Create a new method signature for the body of the original method and modify the
method to use the new object parameters. Compile and test.
4) Replace calls to the old method signature with calls to the new method signature using
the new class for parameters. Consider doing the incrementally if there are many callers,
compiling and testing after you convert each caller.
5) Remove the old method signature. Compile and test.