Inline Class

Merge a class that isn’t doing enough into another.

“A class should do something.”

Reverse of Extract Class.

graph LR
    A[Class 1] -->|Refactoring| B[Combined Class]
    C[Class 2] --> B

When to apply (Smells)

  • Lazy Class: A class that has lost its responsibilities (due to refactoring).
  • Poltergeists: Useless short-lived classes.

Motivation

  • Reduce complexity by removing unnecessary files/classes.

Mechanics (Steps)

  1. Decide which class will absorb the other.
  2. Move fields and methods from the victim class to the absorbing class.
  3. Rewrite references.
  4. Delete the empty class.

Explanation

Problem

A class does too little to justify its existence.

Solution

Move all its features into another class and delete it.

Pros and Cons

Pros Cons
  • Less indirection
  • May create a Large Class if not careful
  • Comparison

    • Inverse Refactoring: Extract Class.

    Code example

    Typescript

    Before

    After

    PHP

    Before

    After