Thursday, February 9, 2012

Common Refactoring Techniques

Here are some examples of code refactoring; some of these may only apply to certain languages or language types. A longer list can be found in Fowler's Refactoring book and on Fowler's Refactoring Website.

  •     Techniques that allow for more abstraction
    • Encapsulate Field – force code to access the field with getter and setter methods
    • Generalize Type – create more general types to allow for more code sharing
    • Replace type-checking code with State/Strategy
    • Replace conditional with polymorphism
  • Techniques for breaking code apart into more logical pieces
    • Extract Method, to turn part of a larger method into a new method. By breaking down code in smaller pieces, it is more easily understandable. This is also applicable to functions.
    • Extract Class moves part of the code from an existing class into a new class.
  • Techniques for improving names and location of code
    • Move Method or Move Field – move to a more appropriate Class or source file
    • Rename Method or Rename Field – changing the name into a new one that better reveals its purpose
    • Pull Up – in OOP, move to a superclass
    • Push Down – in OOP, move to a subclass

Tools that can be used to enhance refactoring technique with visual studio 2005~20xx

Among the above tools Resharper is most popular.

References:

  1. http://en.wikipedia.org/wiki/Code_refactoring
  2. http://msdn.microsoft.com/en-us/library/ms379618%28VS.80%29.aspx

No comments:

Post a Comment