Refactoring
Refactoring
REFACTORING
Jaypee Institute of Institute Technology
declared DEEMED TO BE UNIVERSITY UNDER SECTION 3 OF UGC ACT
Refactoring
• Refactoring is:
– restructuring (rearranging) code in a series of small, semantics-
preserving transformations (i.e. the code keeps working) in order to
make the code easier to maintain and modify
• Refactoring is not just arbitrary restructuring
– Code must still work
– Small steps only so the semantics are preserved (i.e. not a major re-
write)
– Unit tests to prove the code still works
– Code is
• More loosely coupled
• More cohesive modules
• More comprehensible
• There are numerous well-known refactoring techniques
– You should be at least somewhat familiar with these before
inventing your own
– Refactoring “catalog”
Jaypee Institute of Institute Technology
declared DEEMED TO BE UNIVERSITY UNDER SECTION 3 OF UGC ACT
When to refactor
Back to refactoring
Refactoring Process
Code Smells
• If it stinks, change it
– Code that can make the design harder to change
• Examples:
– Duplicate code
– Long methods
– Big classes
– Big switch statements
– Long navigations (e.g., a.b().c().d())
– Lots of checking for null objects
– Data clumps (e.g., a Contact class that has fields for address,
phone, email etc.) - similar to non-normalized tables in relational
design
– Data classes (classes that have mainly fields/properties and
little or no methods)
– Un-encapsulated fields (public member variables)
• Divergent change
• Shotgun surgery
• Lazy class
• Speculative generality
• Feature envy
• Refused Bequest
• Comments
Jaypee Institute of Institute Technology
declared DEEMED TO BE UNIVERSITY UNDER SECTION 3 OF UGC ACT
Example 1, continued
• class Animal {
final int MAMMAL = 0, BIRD = 1, REPTILE = 2;
int myKind; // set in constructor
...
String getSkin() {
switch (myKind) {
case MAMMAL: return "hair";
case BIRD: return "feathers";
case REPTILE: return "scales";
default: return “skin";
}
}
}
Jaypee Institute of Institute Technology
declared DEEMED TO BE UNIVERSITY UNDER SECTION 3 OF UGC ACT
Example 1, improved
class Animal {
String getSkin() { return “skin"; }
}
class Mammal extends Animal {
String getSkin() { return "hair"; }
}
class Bird extends Animal {
String getSkin() { return "feathers"; }
}
class Reptile extends Animal {
String getSkin() { return "scales"; }
Jaypee Institute of Institute Technology
declared DEEMED TO BE UNIVERSITY UNDER SECTION 3 OF UGC ACT
Encapsulating Fields
– Also:
• Rename Method
• Change Method Parameters
Jaypee Institute of Institute Technology
declared DEEMED TO BE UNIVERSITY UNDER SECTION 3 OF UGC ACT
3. Extract Class
• Break one class into two, e.g. Having the phone details as part of
the Customer class is not a realistic OO model, and also breaks
the Single Responsibility design principle. We can refactor this into
two separate classes, each with the appropriate responsibility.
4. Extract Interface
• Extract an interface from a class. Some clients may
need to know a Customer’s name, while others may
only need to know that certain objects can be
serialized to XML. Having toXml() as part of the
Customer interface breaks the Interface Segregation
design principle which tells us that it’s better to have
more specialized interfaces than to have one
multi-purpose interface.
Jaypee Institute of Institute Technology
declared DEEMED TO BE UNIVERSITY UNDER SECTION 3 OF UGC ACT
4. Extract Interface
5. Extract Method
6. Extract Subclass
• When a class has features (attributes and methods) that would only
be useful in specialized instances, we can create a specialization of
that class and give it those features. This makes the original class
less specialized (i.e., more abstract), and good design is about
binding to abstractions wherever possible.
Refactored
public abstract class Party
{
public class Company extends Party
public void PrintNameAndDetails()
{
{
private String name;
printName();
private String
printDetails();
companyType; private Date
}
incorporated; public void
public abstract void printName();
printDetails()
public abstract void printDetails();
{
}
System.out.println("Incorporated: " + incorporated.toString());
}
public class Person extends Party
public void printName()
{
{
private String firstName;
private String lastName; System.out.println("Name: " + name + " " + companyType);
private Date dob; }
private String nationality; }
public void printDetails()
{
System.out.println("DOB: " + dob.toString() + ", Nationality: " + nationality);
}
public void printName()
{
System.out.println("Name: " + firstName + " " + lastName);
}
}
Jaypee Institute of Institute Technology
declared DEEMED TO BE UNIVERSITY UNDER SECTION 3 OF UGC ACT
Duplicated Code
• Extract Method
• Pull up method
• Substitute algorithm
• Extract Classes
Jaypee Institute of Institute Technology
declared DEEMED TO BE UNIVERSITY UNDER SECTION 3 OF UGC ACT
Long Method
• Extract Method
Jaypee Institute of Institute Technology
declared DEEMED TO BE UNIVERSITY UNDER SECTION 3 OF UGC ACT
Large Class
Divergent Change
Shotgun Surgery
Lazy Class
Speculative Generality
Data Class
Feature Envy
Refused Bequest
Comments
More on Refactorings
• Refactoring Catalog
– http://www.refactoring.com/catalog
• Java Refactoring Tools
– NetBeans 4+ – Built In
– JFactor – works with VisualAge and JBuilder
– RefactorIt – plug-in tool for NetBeans, Forte, JBuilder and
JDeveloper. Also works standalone.
– JRefactory – for jEdit, NetBeans, JBuilder or standalone
• Visual Studio 2005+
– Refactoring Built In
• Encapsulate Field, Extract Method, Extract Interface, Reorder
Parameters, Remove Parameter, Promote Local Var to Parameter,
more.