Navs
Code Smells Catalog
Speculative Generality

Last Revision — April 19, 2022

1 Min Read


  • Also Known As

    ---

  • Obstruction

    Dispensables

  • Occurrence

    Unnecessary Complexity

  • Expanse

    Between Classes

  • Related Smells

    - Dubious Abstraction (antagonistic)
    - Lazy Element (causes)
    - Dead Code (causes)

  • History

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

Speculative Generality

Developers are humans, and humans are bad guessers [1]. Developers tend to create additional features in preparation for the future, guessing they will be useful, but that time never came. This problem lies within the human psychological nature and, contrary to their best intentions, it just clutters the code.

Causation

Psychologically, humans tend to anticipate scenarios and prepare for them.

Problems

You Ain't Gonna Need It Principle Violation

The whole system is trying or expecting to do more than it is supposed to.

Increased Complexity

Each additional method, class, or module increases the required time and effort to understand it as a whole.

Examples

Smelly

Context: Medieval Fighting Game

class Animal:
    health: int

class Human(Animal):
    name: str
    attack: int
    defense: int

class Swordsman(Human):
    ...

class Archer(Human):
    ...

class Pikeman(Human):
    ...

Solution

class Human:
    name: str
    health: int
    attack: int
    defense: int

class Swordsman(Human):
    ...

class Archer(Human):
    ...

class Pikeman(Human):
    ...

Refactoring:

  • Collapse Hierarchy
  • Inline Function
  • Inline Class
  • Rename Method

Sources
  • [1] - Leah T. Braun, "Guessing right – whether and how medical students give incorrect reasons for their correct diagnoses" (2019)
  • [Origin] - Martin Fowler, "Refactoring: Improving the Design of Existing Code" (1999)