June 08, 2005
Often times, candidate code for refactoring is based upon a source file's smell. Spotting can take time to acquire and is largely based upon subjective determinations. The proper use of code metrics can facilitate the discovery of candidate code which is in need of refactoring. Some metrics are Cyclomatic Complexity, Fan-In, Fan-Out, and Depth of Inheritance. The knowledge of how to effectively spot smelly code and replace it with proven patterns will ultimately lead to a more stable, maintainable, and elegant code base. Cyclomatic Complexity is adept at spotting methods containing a high degree of conditional logic. This code can be replaced with polymorphism using Martin Fowler's pattern, Refactoring with the Replace Conditional with Polymorphism pattern. Metrics Fan-In and Fan-Out are quite effective at pinpointing brittle code. This brittleness can be refactored into a more stable state with patterns such as Extract Hierarchy and Extract Class. Excessively deep hierarchy trees create problematic testing targets. These can be broken out into separate objects with Fowler's Replace Inheritance with Delegation and Collapse Hierarchy patterns.
The presentation is here.