Extract Class
Category B — Moving features and responsibilities
Create a new class/module for a cohesive subset of data and behavior, then move that subset out of the original class.
Extract Class is the class-level answer to "this thing has more than one reason to change." It makes responsibilities explicit by giving each responsibility a home.
Motivation
The smell
- Multiple responsibility clusters. One group of fields and methods deals with pay; another deals with mailing address; another deals with persistence.
- Data clumps inside a class. Several fields are always passed around or used together and represent a concept.
- Methods change together. A subset of methods is edited for one kind of change, while another subset changes for a different reason.
- Hard-to-name class. The class name is vague (
Manager,Handler,Record) because it owns too many concerns. - Large class with weak cohesion. The class has many fields, but most methods only use a subset.
Fowler's heuristic: a class should be a crisp abstraction, handle a few clear responsibilities, or be easier to understand.
The design principle
- Single Responsibility / cohesion. A class should have one primary reason to change. Fields and methods that belong to the same concept should live together.
- Explicit concepts. When a hidden concept gets a name and type, code becomes easier to navigate, test, and reuse.
- Encapsulation. Moving data with the behavior that operates on it reduces feature envy and scattered rules.
- Behavior preservation. Extraction changes structure, not observable behavior. Public behavior should remain the same unless the refactoring intentionally includes a delegated API change.
Extract Class is not:
- splitting classes arbitrarily by line count;
- creating a new class for every field;
- moving data away from the behavior that needs it.
When not to extract
- The class is already cohesive. Splitting it would create indirection without a clearer model.
- The proposed class has no behavior and no real concept. A dumb data bag may be fine, but do not extract unless it clarifies responsibility.
- The fields/methods are not truly cohesive. If they do not change together or represent one concept, the boundary is wrong.
- The extraction would create tight bidirectional coupling with no clarity gain. Reconsider the boundary.
- A simpler move is enough. Sometimes Extract Function, Move Function, or Introduce Parameter Object solves the problem.
Mechanic
Before you start
- Read the whole class/module and map fields to methods. Look for clusters: which methods use which fields?
- Identify the responsibility to extract and give it a clear name.
- Find existing checks. Use tests/type checker/linter/run paths that exist. Do not add tests unless asked.
- Decide how the old class will relate to the new one: composition/delegation is the default.
Steps
- Choose one cohesive cluster. Pick fields and methods that represent one concept or reason to change.
- Create the new class/module. Name it after the concept, not after
Data,Info, orHelperunless that is truly the domain term. - Move fields first. Copy/move the data into the new class. Initialize it in the old class or accept it as a dependency.
- Move methods next. Start with methods that use only the moved fields. Then move methods that need minor parameter/reference adjustments.
- Preserve the old interface when needed. Keep old methods as thin delegates if callers depend on them. Remove delegation only when safe and in scope.
- Update callers gradually. Prefer small steps: construct/own the new class, delegate, then switch callers.
- Run checks after each step. Especially after moving state and updating construction.
- Clean up. Remove unused fields/methods from the old class, simplify constructor parameters if appropriate, and ensure dependencies point in a sensible direction.
Deciding what moves
| Signal | Action |
|---|---|
| Field is used only by moved methods | Move it to the new class. |
| Field is shared by both responsibilities | Keep it where it fits best; pass it as parameter or expose a narrow accessor. |
| Method uses mostly moved fields | Move the method. |
| Method mixes responsibilities | Extract Function first, then move the extracted part. |
| Old callers need the old method | Keep a delegating wrapper temporarily. |
| New class needs owner data | Pass required data in; avoid back-references unless necessary. |
Rules that keep the move safe:
- Move one cohesive cluster at a time.
- Preserve observable behavior and public contracts unless explicitly changing them.
- Prefer composition over inheritance for the extracted responsibility.
- Avoid bidirectional dependencies; if unavoidable, keep them narrow and intentional.
- Do not mix extraction with feature changes.
Example
Before
class Employee:
def __init__(self, name, base_salary, bonus_rate, street, city, postal_code):
self.name = name
self.base_salary = base_salary
self.bonus_rate = bonus_rate
self.street = street
self.city = city
self.postal_code = postal_code
def annual_pay(self):
return self.base_salary + self.base_salary * self.bonus_rate
def mailing_label(self):
return f"{self.name}\n{self.street}\n{self.postal_code} {self.city}"
The class mixes compensation and mailing concerns.
After
class MailingAddress:
def __init__(self, street, city, postal_code):
self.street = street
self.city = city
self.postal_code = postal_code
def label(self, name):
return f"{name}\n{self.street}\n{self.postal_code} {self.city}"
class Employee:
def __init__(self, name, base_salary, bonus_rate, address):
self.name = name
self.base_salary = base_salary
self.bonus_rate = bonus_rate
self.address = address
def annual_pay(self):
return self.base_salary + self.base_salary * self.bonus_rate
def mailing_label(self):
return self.address.label(self.name)
Employee keeps compensation responsibility; MailingAddress owns address formatting. Behavior is preserved through delegation.
A non-example
Do not extract BaseSalaryCalculator if it only wraps one trivial multiplication and has no real concept. That adds indirection without clarifying the model.
Related refactorings
- Move Function / Move Field — the mechanical moves used during extraction.
- Extract Function — separate mixed responsibilities inside a method before moving them.
- Introduce Parameter Object — useful when a data clump should become a value object rather than a service-like class.
- Inline Class — the inverse, when an extracted class stops earning its keep.
Running this skill on a file
When invoked with @extract_class:
- Confirm the target. Use the file/class named by the user. If none is named, ask for one. Read it completely.
- Map responsibilities. Report field clusters, method clusters, and the proposed new class/module name.
- Inventory candidates. For each candidate, report:
- fields and methods to move,
- why they form a cohesive responsibility,
- dependencies that stay behind,
- risk: low (clear cluster, private/internal), medium (shared fields or many callers), high (public API, serialization/persistence, inheritance).
- Propose before editing. Present the extraction boundary and migration plan. Do not change files on the first pass.
- Apply one extraction at a time. Move fields, then methods, then update callers/delegates. Keep diffs small.
- Run existing checks after each step. If no checks exist, state what was not verified.
- Report. Say what moved, what delegated, what stayed, and any follow-up refactorings.
- Stop and ask if the boundary is unclear, if shared state creates tight coupling, or if public behavior would change.
Checklist
- The new class/module has a clear, cohesive responsibility.
- Moved fields and methods belong together.
- Public behavior is preserved or intentionally delegated.
- Bidirectional dependencies are avoided or explicitly justified.
- Checks pass, or the unverified risk is stated.