Programming Search⌘K

Techniquecore

Pull up method

Move a method that two or more subclasses define in the same way into their common superclass, and remove the copies.

Family
refactoring
Intent
Keep one copy of behavior that all subclasses share.

Purpose

When two subclasses have the same method, a change to the behavior must be made twice. Sooner or later one copy changes and the other does not. This is Duplicated code inside a class hierarchy.

Pull up method moves the method into the superclass, and the subclasses inherit it. The catalogue card has a class Employee with two subclasses, Salesman and Engineer, and each subclass has its own get name(). After the refactoring, get name() is in Employee, and the subclasses have no copy.1 The card names the inverse, Push Down Method.1

Steps

  1. Look at the methods. Make sure that they do the same thing. If they only look alike, first change them until their bodies are the same.
  2. Check that each call and each field that the body uses is in the superclass too. If a field is only in the subclasses, pull it up first with Pull Up Field.
  3. If the signatures differ, make them the same with Change Function Declaration.
  4. Copy the method into the superclass.
  5. Remove the method from one subclass, and test. Then do the same for each other subclass.

Example

Salesman and Engineer both compute the annual cost from the monthly cost. After the refactoring, Employee has the one copy.

import assert from "node:assert/strict";

class Employee {
  constructor(name, monthlyCost) {
    this._name = name;
    this._monthlyCost = monthlyCost;
  }
  get name() { return this._name; }
  get monthlyCost() { return this._monthlyCost; }
  get annualCost() { return this.monthlyCost * 12; }
}

class Salesman extends Employee {}
class Engineer extends Employee {}

// Before, each subclass had its own copy of annualCost.
class SalesmanBefore extends Employee {
  get annualCost() { return this.monthlyCost * 12; }
}

const ana = new Salesman("Ana", 1000);
const bo = new Engineer("Bo", 1500);
assert.equal(ana.annualCost, new SalesmanBefore("Ana", 1000).annualCost);
assert.ok(!Object.hasOwn(Salesman.prototype, "annualCost"));
console.log(ana.annualCost, bo.annualCost);
Output
12000 18000
Language What the example shows
JavaScript annualCost moved from the subclasses to Employee, as above
Python the same move; the subclasses inherit the property from the base class
Go not inheritance: Go has no subclasses, so the shared method goes on a struct that the other structs embed

In languages without class inheritance

Go and Rust do not have class inheritance, so the refactoring does not apply as written.

  • Go. Effective Go says: "Go does not provide the typical, type-driven notion of subclassing, but it does have the ability to “borrow” pieces of an implementation by embedding types within a struct or interface."5 The Go form of Pull up method is to move the method to a struct that both types embed. The method is then promoted to each type.
  • Rust. The Rust book says: "There is no way to define a struct that inherits the parent struct’s fields and method implementations without using a macro."6 The Rust form is a default method in a trait that both types implement, or a free function that both call.
PythonGitHub
class Employee:
    def __init__(self, name: str, monthly_cost: int) -> None:
        self.name = name
        self.monthly_cost = monthly_cost

    @property
    def annual_cost(self) -> int:
        return self.monthly_cost * 12


class Salesman(Employee):
    pass


class Engineer(Employee):
    pass


ana, bo = Salesman("Ana", 1000), Engineer("Bo", 1500)
assert "annual_cost" not in vars(Salesman)
print(ana.annual_cost, bo.annual_cost)
Output
12000 18000
package main

import "fmt"

// Go has no subclasses. The shared method goes on a struct that the others embed.
type Employee struct {
	Name        string
	MonthlyCost int
}

func (e Employee) AnnualCost() int { return e.MonthlyCost * 12 }

type Salesman struct{ Employee }
type Engineer struct {
	Employee
	Level int
}

func main() {
	ana := Salesman{Employee{"Ana", 1000}}
	bo := Engineer{Employee{"Bo", 1500}, 2}
	// AnnualCost is promoted from the embedded Employee.
	fmt.Println(ana.AnnualCost(), bo.AnnualCost())
}
Output
12000 18000

When to use

  • The subclasses have the same method. The body, or the result, is the same in each one.
  • The methods are almost the same. First make them the same, then pull the method up. If they differ in a small step, make that step a method of its own, keep the subclass versions of the step, and pull up the rest. The result is the Template method pattern.
  • After Extract Superclass. The new superclass then takes the shared methods. See Extract Superclass.

Pitfalls

Not all subclasses need it. A method in the superclass is a part of the interface of each subclass. If one subclass must not have the method, pulling it up gives that subclass behavior that it does not want. See Push Down Method.

Hidden differences. Two methods with the same body can still give different results if they call a method that each subclass overrides. This can be correct, but test each subclass after the move.

Tools that do it

IntelliJ IDEA has Pull Members Up. Its documentation says: "The Pull Members Up refactoring lets you move fields if two classes have the same field, or move methods if your subclasses have methods performing similar work."4 Most IDEs for Java, C# and TypeScript have a similar action.

The catalogue lists the refactoring in the first edition, the second edition and the Ruby edition, with the tag dealing-with-inheritance.2 In the second edition it is in Chapter 12, Dealing with Inheritance.3

References

  1. Martin Fowler, Catalog of Refactorings, Pull Up Method, the entry, the Employee, Salesman and Engineer sketch; inverse of Push Down Method
  2. Martin Fowler, Catalog of Refactorings, index, the entry pullUpMethod, its books and tags
  3. Martin Fowler, Refactoring, Improving the Design of Existing Code, second edition, Addison-Wesley, 2018, Chapter 12, Dealing with Inheritance, Pull Up Method
  4. JetBrains, IntelliJ IDEA documentation, Pull members up, push members down, the first paragraphs
  5. The Go Programming Language, Effective Go, Embedding
  6. The Rust Programming Language, Chapter 18.1, Characteristics of Object-Oriented Languages, Inheritance as a Type System and as Code Sharing

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/0P5NED/text.yaml