inappropriate intimacy

**Inappropriate intimacy** is a **code smell where two classes or modules have excessive knowledge of each other's internal details** — characterized by classes that access private fields, use implementation internals, or have bidirectional dependencies that violate encapsulation principles, making code difficult to modify, test, and maintain independently. **What Is Inappropriate Intimacy?** - **Definition**: Code smell where classes are too closely coupled. - **Symptom**: Classes access each other's private/protected members excessively. - **Violation**: Breaks encapsulation and information hiding principles. - **Risk**: Changes to one class force changes to the other. **Why It's a Code Smell** - **Tight Coupling**: Classes cannot change independently. - **Testing Difficulty**: Hard to unit test without the coupled class. - **Maintenance Burden**: Changes ripple across coupled components. - **Reusability Loss**: Can't reuse one class without the other. - **Comprehension Overhead**: Must understand both classes together. - **Circular Dependencies**: Often leads to import/dependency cycles. **Signs of Inappropriate Intimacy** **Direct Symptoms**: - Class A directly accesses Class B's private fields. - Excessive use of friend classes or package-private access. - Classes that "reach through" objects to get deep internal state. - Bidirectional navigation (A references B, B references A). **Code Patterns**: ```java // Inappropriate intimacy - accessing internals class Order { void applyDiscount() { // Accessing Customer's internal pricing data double rate = customer.internalPricingData.getBaseRate(); double tier = customer.loyaltyPoints / customer.POINTS_PER_TIER; } } // Better - ask, don't grab class Order { void applyDiscount() { double discount = customer.calculateDiscountRate(); } } ``` **Refactoring Solutions** **Move Method/Field**: - Move behavior to the class that owns the data. - Reduces cross-class dependencies. **Extract Class**: - Pull shared behavior into a new class. - Both original classes depend on extracted class. **Hide Delegate**: - Create wrapper methods instead of exposing internals. - Callers use interface, not implementation. **Replace Bidirectional with Unidirectional**: - Eliminate one direction of the dependency. - Use callbacks, events, or dependency injection. **Use Interfaces**: - Depend on abstractions, not concrete implementations. - Reduces coupling to specific class internals. **AI Detection Approaches** - **Coupling Metrics**: Measure Coupling Between Objects (CBO). - **Access Pattern Analysis**: Track cross-class field/method access. - **Graph Analysis**: Identify bidirectional edges in dependency graphs. - **ML Classification**: Train models on labeled intimate vs. clean code. **Tools for Detection** - **Code Quality**: SonarQube, CodeClimate detect coupling issues. - **Static Analysis**: NDepend, Structure101, JArchitect. - **IDE Features**: IntelliJ coupling analysis, Visual Studio metrics. - **AI Assistants**: Modern AI code reviewers flag intimacy patterns. Inappropriate intimacy is **a maintainability killer** — when classes know too much about each other's internals, the codebase becomes fragile and resistant to change, making refactoring to clean boundaries essential for long-term software health.

Go deeper with CFSGPT

Get AI-powered deep-dives, save terms, and run advanced simulations — free account.

Create Free Account