Inappropriate Intimacy
Classes that know too much about each other’s internals.
“Sometimes classes become far too intimate and spend too much time delving in each others’ private parts.” – Fowler & Beck
Signs of Use (Symptoms)
- One class accesses another’s private fields (perhaps via reflection or friend access).
- Bidirectional dependencies.
Why it is bad (Consequences)
- Fragility: Change one class, break the other.
- Encapsulation broken: Internals are exposed.
Why it happens (Root Cause)
Classes were developed together and shortcuts were taken.
When it might be okay (Exceptions)
- Helper/Factory classes tightly aligned with the target.
Explanation
Problem
User knows internal details of Session. Session knows internal details of User.
The Flaw
No clear boundaries. Changes ripple.
Real world analogy
Two roommates who share everything, including toothbrushes. Boundaries matter.
Refactoring Solution
- Move Method / Move Field: Reduce dependencies.
- Extract Class: Create intermediary.
- Hide Delegate.
Pros and Cons (of the Antipattern)
| Pros (Why people do it) | Cons (The price you pay) |
|---|---|
Comparison
- Related Antipatterns: Feature Envy.
- Related Principles: Low Coupling, Encapsulation.
Code example
Typescript
Bad (The Antipattern)
Good (The Fix)