Programming Search⌘K

Techniquecore

Push down field

Move a field from a superclass into the one subclass, or the few subclasses, that use it.

Family
refactoring
Intent
Keep data in the class that uses it.

Purpose

A field in a superclass is in each object of each subclass. If only one subclass uses it, the other objects carry an empty value, and a reader of the superclass must ask what the field is for.

Push down field moves the field to the subclass that uses it. The catalogue card shows it in Java: Employee declares private String quota;. After the refactoring, Salesman declares protected String quota;, and Employee and Engineer have no quota.1 The card names the inverse, Pull Up Field.1

The refactoring often goes with Push Down Method: the methods that use the field move down with it.

Steps

  1. Declare the field in each subclass that uses it.
  2. Remove the field from the superclass.
  3. Test.
  4. Remove the field from each subclass that does not use it. Test.

Example

Before the refactoring, each employee object had a _quota field, with null for engineers. After it, only salesmen have the field.

import assert from "node:assert/strict";

class EmployeeBefore {
  constructor(name, quota = null) { this._name = name; this._quota = quota; }
}

class Employee {
  constructor(name) { this._name = name; }
}
class Engineer extends Employee {}
class Salesman extends Employee {
  constructor(name, quota) { super(name); this._quota = quota; }
}

assert.deepEqual(Object.keys(new EmployeeBefore("Bo")), ["_name", "_quota"]);
assert.deepEqual(Object.keys(new Engineer("Bo")), ["_name"]);
console.log(Object.keys(new Salesman("Ana", 50000)));
Output
[ '_name', '_quota' ]
Language What the example shows
JavaScript the _quota field moved from Employee to Salesman, as above
Python the same move; vars shows the attributes of each object
Go not inheritance: the field moves from the embedded struct to the struct of the one type that uses it
PythonGitHub
class Employee:
    def __init__(self, name: str) -> None:
        self.name = name


class Engineer(Employee):
    pass


class Salesman(Employee):
    def __init__(self, name: str, quota: int) -> None:
        super().__init__(name)
        self.quota = quota


assert list(vars(Engineer("Bo"))) == ["name"]
print(list(vars(Salesman("Ana", 50000))))
Output
['name', 'quota']
package main

import "fmt"

type Employee struct{ Name string }

type Engineer struct{ Employee }

// Quota is a field of Salesman only.
type Salesman struct {
	Employee
	Quota int
}

func main() {
	fmt.Printf("%+v\n", Engineer{Employee{"Bo"}})
	fmt.Printf("%+v\n", Salesman{Employee{"Ana"}, 50000})
}
Output
{Employee:{Name:Bo}}
{Employee:{Name:Ana} Quota:50000}

When to use

  • A field that most subclasses leave empty. It is a sign that the field belongs to one subclass.
  • A subclass that does not want what it inherits. See Refused bequest.

Pitfalls

Code in the superclass uses the field. If a method of the superclass reads the field, push that method down first, or the field cannot move.

A field that tells the subclasses apart. If the value of the field is the only difference between subclasses, the subclasses may not be needed. See Remove Subclass.

Tools that do it

IntelliJ IDEA has Push Members Down, which moves "class members to a subclass or a subinterface".4 Most IDEs for Java and C# have a similar action.

The catalogue lists the refactoring in the first and the second 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, Push Down Field, the entry, the Java quota sketch; inverse of Pull Up Field
  2. Martin Fowler, Catalog of Refactorings, index, the entry pushDownField, its books and tags
  3. Martin Fowler, Refactoring, Improving the Design of Existing Code, second edition, Addison-Wesley, 2018, Chapter 12, Dealing with Inheritance, Push Down Field
  4. JetBrains, IntelliJ IDEA documentation, Pull members up, push members down, Push members down

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/KX6AKP/text.yaml