Insider Trading — Code Smells Catalog Skip to content

Insider Trading

Also known as: Inappropriate Intimacy

Data Dealers Responsibility Code SmellDesign Smell Between Class

Two classes exchanging private implementation details they shouldn't have access to — the kind of under-the-table knowledge sharing that makes either one impossible to change without breaking the...

2 min read 2 sources

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

🔗
Coupling

The modules between themselves should know as little as possible.

Reduced Reusability

Developers cannot reuse intertwined classes in isolation.

🧪
Hard to Test

Mocking is required.

Example

Smelly
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)
PYTHON

Refactoring

  • Move Method
  • Move Field
  • Encapsulate Field
  • Replace Inheritance with Delegation
  • Change Bidirectional Association to Unidirectional

Sources

Browse All 56 Smells