Refactoring

Bad Smells in Code
Smell Refactorings Priority
Alternate Class With Different Interfaces
  • Rename Method
  • Move Method
Combinatorial Explosion
  • Tease Apart Inheritance
  • Replace Inheritance with Delegation
Comments
  • Extract Method
  • Introduce Assertions
  • Rename Method
1
Data Class
  • Move Method
  • Encapsulate Field/Collection
Data Clumps
  • Extract Class
  • Introduce Parameter Object
  • Preserve Whole Object
Divergent Change
  • (One class needs to be changed in different ways for different reasons) Extract Class for each change – you feel better to have two classes handling changes separately than changing some methods of One class for several changes.
Duplicate Method
  • Extract Method
  • Extract Class
  • Pull Up Method
  • Form Template Method
Feature Envy
  • Move Field
  • Move Method
  • Extract Method
Inappropriate Intimacy
  • Move Method/Field
  • Change Bi-Directional to Uni-Directional Association
  • Replace Inheritance with Delegation
  • Hide Delegate
Incomplete Library Class
  • Introuce Foreign Method (helper method on a library class – use the libray class as first parameter)
  • Introduce Local Extension (lot of helper method’s for library class – extend the library class and use it).
Large Class
  • Extract Class
  • Extract Subclass
  • Extract Interface
  • Extract Domain Object (Duplicate Observed Data)
3
Lazy Class
  • Inline Class
  • Collapse Hierarchy
Long Method (Clean up straight-line code)
  • Extract Method (comments -OR- white lines  that deliniate code)
  • Replace temp with query
  • Replace Method with Method Object (Convert all the temp variables on the method to fields of a Object and use them to call the method)
  • Decompose Conditional (extract conditional logic to named method)
2
Long Parameter List
  • Replace Parameter with Method
  • Preserve the Whole Object
  • Introduce Parameter Object
4
Message Chains
  • Hide Delegate. (for all client calls to the delegates, let the server deal with the delegate and expose the API to client ex: employee.getDepartment().getManager())
  • Tell Don’t Ask (Law of Demeter) ex: a.b().c().d() (directly or indirectly).
Middle Man
  • Inline Method
  • Replace Delegation with Inheritance
  • Remove Middle Man
Parallel Inheritance Hierarchies
  • Move Method
  • Move Field
Primitive Obsession
  • Replace Data Value with Object
  • Replace Array with Object etc.,
Refusal Bequest
  • Replace Inheritance with Delegation (subclass only uses part of superclass or only inherits part of data).
Shotgun Surgery
  • (Opposite to Divergent Change. For one change lot of small changes to lot of classes) Move Method/Field
  • Inline Class.
Speculative Generality
  • Collapse Hierarchy
  • Inline Class
  • Remove Parameter
  • Rename Method
Switch Statements
  • Replace Conditional with Polymorphism
  • Replace Type code with Class
  • Replace Type code with Subclasses
  • Replace Type Code with State/Strategy
  • Replace parameter with Explicit Methods
  • Introduce Null Object (replace checks for null references with Null Objects).
Temporary Field
  • Extract Class
  • Introduce Null Object
Reference:

  1. http://martinfowler.com/books/#refactoring
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s