Collapse Hierarchy

A superclass and subclass are not very different.

“A superclass and subclass are not very different.”

Merge them.

graph LR
    A[Superclass] -->|Refactoring| C[Combined Class]
    B[Subclass] --> C

When to apply (Smells)

  • Lazy Class: A subclass that adds no value.

Motivation

  • Simplicity: Why have two classes when one will do?

Mechanics (Steps)

  1. Select which one to remove (usually subclass).
  2. Pull up everything.
  3. Remove subclass.

Explanation

Problem

Employee and Salesman. Salesman has nothing special anymore.

Solution

Move everything to Employee and delete Salesman.

Pros and Cons

Pros Cons
  • Simplicity
  • Loss of specificity (maybe?)
  • Code example

    Typescript

    Before

    After

    PHP

    Before

    After