Inappropriate Intimacy

When classes are overly interconnected, they share too much information with each other.

Sign of Smell

Inappropriate Intimacy is a code smell that arises when two classes are excessively dependent on each other, resulting in a tight coupling. This can happen when classes become overly intertwined and spend an excessive amount of time accessing each other's private data or methods.

Inheritance can often lead to over-intimacy and cause this smell. Subclasses sometimes know more about their parents than their parents would like them to know.

Inappropriate Intimacy refers to a class that is excessively dependent on another class. If a method is excessively dependent on a class, consider the previous smell called "Feature Envy.”

Reason of Smell

  • High Coupling: Inappropriate intimacy often leads to high coupling between classes. High coupling means that changes in one class can impact other classes, causing a chain reaction of changes. This makes the codebase fragile and difficult to maintain.

  • Violation of Encapsulation: Classes should hide internal details and expose a defined interface. Inappropriate intimacy occurs when one class accesses another class's internal state or methods directly, bypassing encapsulation.

  • Law of Demeter: The Law of Demeter, also called the "principle of least knowledge," states that a class should have limited knowledge of another class's internal knowledge. Inappropriate Intimacy refers to a class excessively relying on and knowing about another class's internals, instead of interacting through well-defined interfaces.

  • Reduced Reusability: When classes are intimately connected, they become less reusable. Reusing one class in another context may require bringing along all the classes it's intimately connected to, creating unnecessary dependencies.

  • Complexity: Code with inappropriate intimacy tends to be more complex and harder to understand. Developers need to keep track of the interactions and dependencies between classes, making it challenging to debug and maintain.

  • Testing Challenges: Unit testing becomes more challenging because it's difficult to isolate the behavior of a single class when it's closely tied to other classes. This can hinder the adoption of effective testing practices.

Refactoring Recipes

  • Change Bidirectional Association to Unidirectional Association

  • Extract Class

  • Move Method

  • Move Field

  • Hide Delegate

  • Replace Inheritance with Delegation

Change Bidirectional Association to Unidirectional Association

When you have a bidirectional association between classes, but one of the classes does not use the features of the other, it is worth considering removing the unused association. This can help simplify the codebase and reduce unnecessary dependencies. By removing the unused association, you can improve the overall clarity and maintainability of the code.

Extract Class

The Extract Class refactoring technique can solve the issue of Inappropriate Intimacy by separating closely related responsibilities and data into a new class. This approach helps to reduce excessive interdependence and coupling between classes.

Move Method

When we encounter two classes that are tightly coupled, we can attempt to address this issue by moving a method from one class to another where it logically belongs. This approach helps to reduce the interdependence between the classes.

Move Field

Similar to the "Move Method" skill, this skill involves moving a field from one class to another where it logically belongs. This approach can also help reduce the interdependence between the classes.

Hide Delegate

The "Hide Delegate" refactoring skill is used to address the Inappropriate Intimacy code smell by encapsulating access to another class within a delegating method or property. This hides the direct connection between classes, reducing interdependence and promoting better encapsulation.

However, it introduces a "Middle Man" between the two classes, which could potentially cause another code smell.

Replace Inheritance with Delegation

This refactoring skill can be used to address the "Inappropriate Intimacy" code smell, specifically when it involves inheritance relationships between classes. This is especially relevant when a subclass exhibits inappropriate intimacy with its parent class. Instead of inheriting behavior from another class, you can replace it with delegation.

Reference

http://teddy-chen-tw.blogspot.com/2014/05/10inappropriate-intimacy.html

https://www.informit.com/articles/article.aspx?p=1400866&seqNum=17

https://code-smells.com/couplers/inappropriate-intimacy

https://refactoring.guru/smells/inappropriate-intimacy

https://refactoring.guru/change-bidirectional-association-to-unidirectional