You are on page 1of 9

Refactoring

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.

Refactoring, Martin Fowler, 1999


Code Smells identify possible problems in software. Code Climate indicates
some of these in your team's software.

Duplicated Code Lazy class


Long method Speculative Generality
Long Parameter List Temporary Field
Large Class Message Chains
Divergent Change Middle Man
Shotgun Surgery Inappropriate Intimacy
Feature Envy Alternative Classes with Different Interfaces
Data Clumps Incomplete Library Class
Primitive Obsession Data Class
Switch Statements Refused Bequest
Parallel inheritance hierarchies Comments
Refactoring, Martin Fowler, 1999
Use these approaches to address some of the basic code smells commonly
found in code.

Duplicated Code Long Method Large Class Long Parameter List


Same code structure Shortest methods live Trying to do too Complicated
in more than one the best and longest much, with too many interfaces are harder
place instance variables, to use
and duplication.

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.

Refactoring, Martin Fowler, 1999


Extract Method is a common refactoring that turns a code fragment into a
method whose name explains the purpose of the method.

1) Create a new method, name it after intention.


2) Copy extracted code from source into new method.
3) Scan code for references to variables that are local in scope to source method - local
variables and parameters for new method.
4) See whether any temporary variables are only used within the extracted code and
declare in new method.
5) Look to see whether any of these local variables are modified by the extracted code.
May need to use a different approach.
6) Pass into the target method as parameters the local-scope variables that are read from
the extracted code. Compile when you have dealt with locally scoped variables.
7) Replace the extracted code in the source methods with a call to the new methods.
8) Remove temporary variables defined and used in the new method from the source
methods. Compile and test.
Refactoring, Martin Fowler, 1999
Introduce Parameter Object replaces a group of parameters that naturally go
together with an object.

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.

Refactoring, Martin Fowler, 1999


Extract Class creates a new class, moving the relevant fields and method from
the old class to the new class.

1) Decide how to split the responsibilities of the class


2) Create a new class to express the split-off responsibilities
3) Make a link from the old class to the new class
4) Use Move Field on each field you wish to move. Compile and test after each move.
5) Use Move Method to move methods. Start with lower-level methods (called rather than
calling) and build to the higher level. Compile and test after each move.
6) Review and reduce the interfaces in each class.
7) Decide whether to expose the new class. If you do expose the class, decide whether to
expose it as a reference object or as an immutable value object.

Refactoring, Martin Fowler, 1999


Many smells have a variety of possible refactoring methods.

Smell Common Refactoring Approaches


Duplicated Code Extract Method, Extract Class, Pull Up Method, Form Template Method
Large Class Extract Class, Extract Subclass, Extract Interface, Replace Data Value with Object
Extract Method, Replace Temp with Query, Replace Method with Method Object, Decompose
Long Method
Conditional
Long Parameter List Introduce Parameter Object, Replace Parameter with Method, Preserve Whole Object
Alternative Classes with
Rename Method, Move Method
Different Interfaces
Comments Extract Method, Introduce Assertion
Data Class Move Method, Encapsulate Field, Encapsulate Collection
Data Clumps Extract Class, Introduce Parameter Object, Preserve Whole Object
Divergent Change Extract Class
Feature Envy Move Method, Move Field, Extract Method
Move Method, Move Field, Change Bidirectional Association to Unidirectional, Replace
Inappropriate Intimacy
Inheritance with Delegation, Hide Delegate

Refactoring, Martin Fowler, 1999


Some additional smells and their possible refactorings.

Smell Common Refactorings


Incomplete Library Class Introduce Foreign Method, Introduce Local Extension
Lazy Class Inline Class, Collapse Hierarchy
Message Chains Hide Delegate
Middle Man Remove Middleman, Inline Method, Replace Delegation with Inheritance
Parallel Inheritance
Move Method, Move Field
Hierarchies
Replace Data Value with Object, Extract Class, Introduce Parameter Object, Replace Array with
Primitive Obsession Object, Replace Type Code with Class, Replace Type Code with Subclasses, Replace Type Code
with State/Strategy
Refused Bequest Replace Inheritance with Delegation
Shotgun Surgery Move Method, Move Field, inline Class
Speculative Generality Collapse Hierarchy, Inline Class, Remove Parameter, Rename Method
Replace Conditional with Polymorphism, Replace Type Code with Subclasses, Replace Type Code
Switch Statements
with State/Strategy, Replace Parameter with Explicit Methods, Introduce Null Object
Temporary Field Extract Class, Introduce Null Object

Refactoring, Martin Fowler, 1999

You might also like