Professional Documents
Culture Documents
Presented By:
Fuad Bin Omar Rashed Kibria
fuad@code71.com rashed@code71.com
www.code71.com www.code71.com
Smells to be covered
•Duplicated code
•Long method
•Long parameter list
•Divergent change
•Shotgun surgery
Duplicated Code
Smell
• Same code structure in more than
one place.
Refactor
Extract method and invoke it from
both the places.
Extract Method
• You have a code fragment that can
be grouped together.
• Turn the fragment into a method
whose name explains the
purpose of the method.
Extract Method (Contd.)
void printOwing()
{
printBanner();
//print details
System.out.println ("name: " + _name);
System.out.println ("amount " +
getOutstanding());
}
Extract Method (Contd.)
void printOwing()
{
printBanner();
printDetails(getOutstanding());
}
void printDetails (double outstanding)
{
System.out.println ("name: " + _name);
System.out.println ("amount " +
outstanding);
}
Duplicated Code
Smell
• same expression in two sibling
subclasses
Refactor
• Extract method in both classes.
• Pull up field.
Pull Up Field
• Two subclasses have the same field.
• Move the field to the superclass.
Pull Up Field (Contd.)
Duplicated Code
Smell
• Code is similar but not the same
Refactor:
• Use Extract Method to
separate the similar bits from the
different bits.
• Use Form Template Method.
Form template method
• You have two methods in subclasses
that perform similar steps in the same
order, yet the steps are different.
• Get the steps into methods with
the same signature, so that the
original methods become the
same. Then you can pull them up.
Form Template Method
(Contd.)
Duplicated Code
Smell
• Methods do the same thing with a
different algorithm
Refactor:
• Choose the clearer of the two
algorithms Use
• Use Substitute Algorithm.
Substitute Algorithm
• You want to replace an algorithm
with one that is clearer.
• Replace the body of the method
with the new algorithm.
Substitute
Algorithm(Contd.)
String foundPerson(String[] people)
{
for (int i = 0; i < people.length; i++)
{
if (people[i].equals ("Don")){ return "Don"; }
if (people[i].equals ("John")){ return "John"; }
if (people[i].equals ("Kent")){ return "Kent"; }
}
return "";
}
Substitute Algorithm
(Contd.)
String foundPerson(String[] people)
{
List candidates = Arrays.asList(new String[] {"Don",
"John", "Kent"});
for (int i=0; i<people.length; i++)
if (candidates.contains(people[i]))
return people[i]; return "";
}
Duplicated Code
Smell
• Duplicated code in two unrelated class.
Refactor:
• Use Extract Class in one class.
• Use the new component to the other.
• Decide where the method makes sense.
Extract Class
• You have one class doing work that
should be done by two.
• Create a new class and move the
relevant fields and methods from
the old class into the new class.
Extract Class (Contd.)
Long method
• Object program having short
methods live best and longest.
• Little methods are the most valuable.
• Longer methods are difficult to
understand.
Long method
• Give methods a good name.
• Whenever you feel the need to comment
something make it a method.
– Group of lines
– Even if it is a single line
– Even if method call is longer than code itself.
– Method length is not the key here.
– What the method does and how it does it is
important.
Long method
• Extract Method
Long method
Smell
Use of temporary variable.
Refactor
Replace temp with query.
Replace temp with query
double basePrice()
{
return _quantity * _itemPrice;
}
Long method
Smell
Methods with long list of parameters.
Refactor
Introduce Parameter Object
Preserve Whole Object
Method with method Object
Introduce Parameter Object
Refactor
• Use Replace Parameter with Method, Preserve
whole object or Introduce Parameter Object.
Replace Parameter With
Method
int basePrice = _quantity * _itemPrice;
discountLevel = getDiscountLevel();
double finalPrice =
discountedPrice (basePrice,discountLevel);
Refactor
• Use Extract class by identifying everything that changes
for a particular cause and put them all together.
Shotgun Surgery
Smell
• A small changes in the code force changes in different classes.
Refactor
• Use Move Method and Move Field to put all the changes into
a single class.
• If no current class looks like a good candidate, create one.
• use Inline Class to bring a whole bunch of behavior together.
Move Method
2. www.refactoring.com