Inappropriate Intimacy#

Inappropriate Intimacy

Inappropriate Intimacy is a code smell that occurs when two classes are too tightly coupled or dependent on each other. This smell indicates that the classes have a high level of intimacy, sharing too much information, or relying on each other’s internal details. This can lead to several problems, including reduced encapsulation, increased complexity, and difficulties in maintaining or evolving the codebase.

The term “Inappropriate Intimacy” highlights the fact that the classes are too close or intertwined, violating the principles of encapsulation and modularity. In a well-designed system, classes should communicate through well-defined interfaces and should not expose their internal details to other classes.

Inappropriate Intimacy can lead to several issues:

  • High Coupling: When two classes are tightly coupled, changes in one class can have a cascading effect on the other. This tight coupling makes the classes harder to maintain and evolve independently.

  • Reduced Encapsulation: Classes that are too intimate may expose their internal details to each other, violating the principle of encapsulation. This can lead to dependencies on specific implementations and reduce the flexibility of the system.

  • Maintenance Difficulty: Intimate classes are more likely to be affected by changes in each other, making it harder to predict the impact of modifications. This can result in unintended side effects and increase the risk of errors.

  • Complexity: High intimacy between classes can make the code harder to understand and reason about. Developers may need to consider the interactions between classes in addition to their individual behavior, increasing the cognitive load.

Solution#

  • Hide Delegate: If one class is accessing the internal details of another class, consider introducing an intermediary class that acts as a delegate. This delegate class can encapsulate the interactions between the two classes and provide a cleaner interface.

  • Move Field

  • Move Method

  • Change Bidirectional Association to Unidirectional: If two classes have a bidirectional association, consider changing it to a unidirectional association. This can help reduce the level of intimacy between the classes and make the relationship clearer.