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)

  1. Check methods are identical.
  2. Move method to superclass.
  3. Delete from subclasses.

Explanation

Problem

Two subclasses have the same getName() method.

Solution

Move getName() to the parent.

Pros and Cons

Pros Cons
  • DRY
  • None
  • Code example

    Typescript

    Before

    After

    PHP

    Before

    After