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)
- Decide which class will absorb the other.
- Move fields and methods from the victim class to the absorbing class.
- Rewrite references.
- 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 |
|---|---|
Comparison
- Inverse Refactoring: Extract Class.
Code example
Typescript
Before
After
PHP
Before
After