Course contentsShow
← Software Engineering
Lesson 350 of 1,93310. RefactoringFree lesson

Extract Class and Inline Class

Split bloated classes into focused ones or merge overly fragmented classes to achieve appropriate responsibility boundaries.

Extract Class and Inline Class

What you'll learn: How to split bloated classes into focused ones or merge overly fragmented classes to achieve appropriate responsibility boundaries.

The Core Idea

Classes, like people, work best when they have a clear, manageable set of responsibilities. Sometimes a class grows too large and tries to do too much (like a person juggling ten tasks at once). Other times, you split things too finely and end up with tiny classes that barely do anything meaningful (like having separate people to open a door, walk through it, and close it).

Extract Class means taking a subset of fields and methods from an oversized class and moving them into a new, separate class. You do this when one class is handling multiple distinct responsibilities.

Inline Class is the reverse: when a class is doing so little that it doesn't justify existing separately, you fold its responsibilities back into another class.

When to Use Each

Extract Class when:

  • A class has grown too large (remember the "Large Class" smell?)
  • A subset of data and methods always work together
  • Changes to one responsibility frequently don't affect the other parts

Inline Class when:

  • A class barely does anything anymore
  • Its responsibilities naturally belong with another class
  • The separation adds complexity without adding clarity

The Balance

Think of a kitchen: you might extract "baking tools" into a separate drawer when your utensil drawer gets crowded. But if you only own one whisk, creating a dedicated "whisk drawer" is overkill—just inline it back into the general tools.

Key Takeaway: Extract Class when responsibilities diverge and a class becomes bloated; Inline Class when separation creates unnecessary fragmentation—both refactorings help you find the right-sized responsibility boundaries for maintainable code.