Navs
Code Smells Catalog
Insider Trading

Last Revision — April 19, 2022

2 Min Read


  • Also Known As

    Inappropriate Intimacy

  • Obstruction

    Data Dealers

  • Occurrence

    Responsibility

  • Expanse

    Between Classes

  • Related Smells

    - Fate over Action (caused)
    - Feature Envy (co-exist)

  • History

    Martin Fowler in book (2018): "Refactoring: Improving the Design of Existing Code (3rd Edition)"

    Martin Fowler in book (1999): "Refactoring: Improving the Design of Existing Code"

Insider Trading

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

@dataclass
class Commit:
    name: str

    def push(self, repo: Repo):
        repo.push(self.name)

    def commit(self, url: str):
        ...


@dataclass
class Repo:
    url: str

    def push(self, name: str):
        ...

    def commit(self, commit: Commit):
        commit.commit(self.url)

Refactoring:

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

Sources
  • [1] [Parentage] - Martin Fowler, "Refactoring: Improving the Design of Existing Code" (1999)
  • [2] - William C. Wake, "Refactoring Workbook" (2004)
  • [Origin] - Martin Fowler, "Refactoring: Improving the Design of Existing Code (3rd Edition)" (2018)