Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Inappropriate Intimacy and Feature Envy?

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?

like image 732
Chen OT Avatar asked Apr 26 '14 13:04

Chen OT


People also ask

What is a feature envy?

Feature envy is defined as occurring when a method calls methods on another class more times than on the source class. It often indicates that the intended functionality is located in the wrong class. Calls to library classes, parent classes, and contained or containing classes are not counted by this inspection.

What is inappropriate intimacy?

InappropriateIntimacy is a CodeSmell that describes a method that has too much intimate knowledge of another class or method's inner workings, inner data, etc.

What is middleman smell code?

A Middle Man is a class that in responsible, principally, for delegation. A class with a Middle Man smell increases the complexity of the code without contributing the program's functionality, as well as allows code to avoid logical flows of data based the relationship between classes.

What is speculative generality?

Speculative generality is a smell to which we are very sensitive. You get it when people say, "Oh, I think we need the ability to do this kind of thing someday" and thus want all sorts of hooks and special cases to handle things that aren't required. The result often is harder to understand and maintain.


1 Answers

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.

like image 67
Dave Schweisguth Avatar answered Oct 13 '22 22:10

Dave Schweisguth