Overview
The classes should know as little as possible about each other. As Fowler put it, “classes spend too much time delving in each other’s private parts” [1]. This code smell was listed in 1999 as Inappropriate Intimacy and is no longer listed in the 2018 version of the book under the same name. It was replaced by the term Insider Trading code smell, possibly to make the change - that now the classes were generalized to modules - more noticeable (similarly to the wording change in Feature Envy).
The concept stays the same - instead of reaching for each other’s secrets, modules/classes interchange too much information and implementation details. In other words, this occurs whenever a module/class has too much knowledge about the inner workings or data of another module/class.
Causation
At first, two classes could intertwine, but over time, they have coupled. [2]
Problems
The modules between themselves should know as little as possible.
Developers cannot reuse intertwined classes in isolation.
Mocking is required.
Example
1@dataclass
2class Commit:
3 name: str
4
5 def push(self, repo: Repo):
6 repo.push(self.name)
7
8 def commit(self, url: str):
9 ...
10
11
12@dataclass
13class Repo:
14 url: str
15
16 def push(self, name: str):
17 ...
18
19 def commit(self, commit: Commit):
20 commit.commit(self.url)Refactoring
- Move Method
- Move Field
- Encapsulate Field
- Replace Inheritance with Delegation
- Change Bidirectional Association to Unidirectional
Sources
- UPDATE2018 · ISBN 978-0134757681
- ORIGIN1999 · ISBN 978-0201485677