Feature Envy

Feature envy reveals a method (or method-to-be) that would work better on a different class.

Symptoms

  • Repeated references to the same object.
  • Parameters or local variables which are used more than methods and instance variables of the class in question.
  • Methods that includes a class name in their own names (such as invite_user).
  • Private methods on the same class that accept the same parameter.
  • Law of Demeter violations.
  • Tell, Don’t Ask violations.

Solutions

  • Extract Method⭐ if only part of the method suffers from feature envy, and then move the method.
  • Move Method⭐ if the entire method suffers from feature envy.
  • Inline Class if the envied class isn’t pulling its weight.

Prevention

  • Follow Law of Demeter limit the dependencies of each method.
  • Follow Tell, Don’t Ask avoiding unnecessary inspection of another object’s state.