widged.com
 

Refactoring

Refactoring refers to continuous improvement of code, facilitated by unit tests, not stopping a project to do a redesign

Test Driven Development by Peter Gordon (ppt)

  • Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behaviour.
  • A series of small behaviour preserving transformations.
  • A sequence of transformations can produce a significant restructuring.
  • Since each refactoring is small, it's less likely to go wrong.
  • The system is kept fully working after each small step, reducing the chances that a system can get seriously broken during the restructuring.

List of Refactorings

Test Driven Development by Peter Gordon (ppt)

* Add Parameter

  • Change Bidirectional Association to Unidirectional
  • Change Reference to Value
  • Change Unidirectional Association to Bidirectional
  • Change Value to Reference
  • Collapse Hierarchy
  • Consolidate Conditional Expression
  • Consolidate Duplicate Conditional Fragments
  • Convert Dynamic to Static Construction by Gerard M. Davison
  • Convert Static to Dynamic Construction by Gerard M. Davison
  • Form Template Method
  • Hide Delegate
  • Decompose Conditional
  • Duplicate Observed Data
  • Encapsulate Collection
  • Encapsulate Downcast
  • Encapsulate Field
  • Extract Class
  • Extract Interface
  • Extract Method
  • Extract Package by Gerard M. Davison
  • Extract Subclass
  • Extract Superclass

From a code point of view, one should aim for

Quoted from Resetting the 'Shitty' Counter @agiletips

  • Short methods (not more then 10 lines per method)
  • Single Responsibility Principle per Class
  • Low complexity (McCabe or CCN)
  • Loose Coupling and High Cohesion
  • Law of Demeter
  • Injectability (ie. Spring, Google Guice, PicoContainer)

Static Code Metrics

  • Test Coverage (use with caution unless you have many small tests)
  • Testability of Code (Testability Explorer) [4]
  • Percentage of duplicated code (CPD) [5]

These are the tools of the trade of an agile test infected code warrior. If you use those best practices on a daily basis and refactor your code mercilessly. Over time your code base will actually become better. It is not easy, it is a skill you learn by doing manually, day in day out.

 
en/techniques/programming/practice/refactor/refactor.txt · Last modified: 2010/12/12 14:00 by marielle
 
RSS - Banner by widged, template © 7throot HeadQuarters, 2007