Feature envy smell

From CSSEMediaWiki
Revision as of 04:19, 19 October 2010 by Jenny Harlow (Talk | contribs)
Jump to: navigation, search

The feature envy smell is present when a class or a method or methods from that class use methods from another class excessively, in particular if a method seems more 'interested' in another class than its own. The most common focus of the envy is the data. This is a good sign that some refactoring should be applied.

Discretion still needs to be applied since particular library methods may get used a lot, but this doesn't necessarily mean we should try to refactor around this.

Refactoring Methods

  • Move Method - if a particular method is using methods from another class excessively, move it to that class
  • Extract Method - if only a part of a method is envious
  • Extract Class - if several methods are envious and they don't belong in the envied class either

Opposing forces

Martin Fowler (Refactoring : improving the design of existing code noted that there are some patterns that break this rule:
"Both Strategy Pattern and Visitor Pattern do this. These are used to combat the Divergent change smell. "The fundamental rule of thumb is to put things together that change together. Data and behaviour that references that data usually change together but there are exceptions. When the exceptions occur, we move the behavior to keep changes in one place. Strategy and Visitor allow you to change behavior easily, because they isolate the small amount of behavior that needs to be overridden, at the cost of further indirection." Martin Fowler 1999

See Also


Personal tools