Pull Up Method
You have methods with identical results on subclasses.
“You have methods with identical results on subclasses.”
Move it to the superclass.
graph LR
A[Subclass A::method()] -->|Refactoring| B[Superclass::method()]
C[Subclass B::method()] --> B
When to apply (Smells)
- Duplication: Same logic in sibling classes.
Motivation
- DRY: Don’t duplicate logic.
Mechanics (Steps)
- Check methods are identical.
- Move method to superclass.
- Delete from subclasses.
Explanation
Problem
Two subclasses have the same getName() method.
Solution
Move getName() to the parent.
Pros and Cons
| Pros | Cons |
|---|---|
Code example
Typescript
Before
After
PHP
Before
After