Both smells are described in Fowler's book "Refactoring".
I know the meanings of those smells are, briefly:
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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With