Domanda

Both smells are described in Fowler's book "Refactoring".

I know the meanings of those smells are, briefly:

  • Feature Envy is that a method in one object invokes half-a-dozen getting methods on another object.
  • Inappropriate Intimacy is that two classes depend on each others' private parts too often.

It looks like both smells indicate that part of one object depends on the other object too much.

Could someone explain the main difference between these two smells?

È stato utile?

Soluzione

You described it pretty well.

Inappropriate Intimacy means compromising the other class's encapsulation, such as by directly accessing instance variables that aren't meant to be directly accessed. Very bad. Fix the grabby class to only use public features of the compromised class and, if possible, change the compromised class so that other classes can't get at its private features.

Feature Envy is when a method uses more public features of another class than it does of its own. Not as bad, because (assuming the other class's public features are safe to use) it won't lead to bugs. But it does lead to design entangling between the two classes. Fix by adding higher-level (better abstracted) public features to the envied class, or moving methods from the envious class to the envied class, so that the envious class has less methods to call.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top