Techniquecore
Pull up field
Move a field that two or more subclasses declare and use in the same way into their common superclass.
- Family
- refactoring
- Intent
- Declare shared data once, in the class that all users share.
Purpose
Subclasses are often written at different times, by different people. Two of them can end up with the same field under the same or a different name. Each subclass then has its own code to read and write the field, and the methods that use it cannot move to the superclass.
Pull up field declares the field once, in the superclass. The catalogue card shows it in Java: Salesman and Engineer each declare private String name;. After the refactoring, Employee declares protected String name;, and the subclasses have no declaration.1 The card names the inverse, Push Down Field.1
The refactoring often comes before Pull Up Method, because a method can only move up when the fields that it uses are there.
Steps
- Look at each use of the field in each subclass. Make sure that it means the same thing in each one.
- If the names differ, rename the fields to one name.
- Declare the field in the superclass. In a language with access control, make it visible to the subclasses, for example
protectedin Java and C#. - Remove the field from the subclasses.
- Test.
Example
In JavaScript, a field is made when a constructor assigns to it. Each subclass set this._name in its own constructor. After the refactoring, the superclass sets it.
import assert from "node:assert/strict";
class EmployeeBefore {}
class SalesmanBefore extends EmployeeBefore {
constructor(name) { super(); this._name = name; }
}
class EngineerBefore extends EmployeeBefore {
constructor(name) { super(); this._name = name; }
}
class Employee {
constructor(name) { this._name = name; }
get name() { return this._name; }
}
class Salesman extends Employee {}
class Engineer extends Employee {}
assert.equal(new Salesman("Ana").name, new SalesmanBefore("Ana")._name);
assert.equal(new Engineer("Bo").name, new EngineerBefore("Bo")._name);
console.log([new Salesman("Ana"), new Engineer("Bo")].map((e) => e.name));
[ 'Ana', 'Bo' ]
| Language | What the example shows |
|---|---|
| JavaScript | the assignment of _name moved to the constructor of Employee, as above |
| Python | the same move; the base class __init__ sets the attribute |
| Go | not inheritance: the field moves to a struct that both types embed, and it is promoted to each one |
class Employee:
def __init__(self, name: str) -> None:
self.name = name
class Salesman(Employee):
pass
class Engineer(Employee):
pass
print([e.name for e in (Salesman("Ana"), Engineer("Bo"))])
['Ana', 'Bo']
package main
import "fmt"
// The shared field is declared once, in a struct that both types embed.
type Employee struct{ Name string }
type Salesman struct{ Employee }
type Engineer struct{ Employee }
func main() {
ana := Salesman{Employee{Name: "Ana"}}
bo := Engineer{Employee{Name: "Bo"}}
// Name is promoted from the embedded Employee.
fmt.Println([]string{ana.Name, bo.Name})
}
[Ana Bo]
When to use
- Subclasses declare the same data. Look for fields with the same type and a similar name, and check how each class uses them.
- Before pulling up methods. The methods that use the field can then move too.
- After Extract Superclass. See Extract Superclass.
Pitfalls
Same name, other meaning. Two fields called rate can hold an hourly rate and a tax rate. Pulling them up joins two different ideas. Check the uses, not only the names.
Wider visibility. A field that was private in each subclass becomes protected in the superclass, so more code can change it. If the superclass can own all the code that changes the field, keep it private there and give the subclasses a getter.
Go and Rust. These languages have no class inheritance. In Go, embedding gives the same effect for fields. Effective Go says that Go can "“borrow” pieces of an implementation by embedding types within a struct or interface."5 In Rust, put the shared field in a struct that each type holds as a field.
Tools that do it
IntelliJ IDEA has Pull Members Up, which "lets you move fields if two classes have the same field".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
- Martin Fowler, Catalog of Refactorings, Pull Up Field, the entry, the Java sketch with Salesman and Engineer; inverse of Push Down Field
- Martin Fowler, Catalog of Refactorings, index, the entry pullUpField, its books and tags
- Martin Fowler, Refactoring, Improving the Design of Existing Code, second edition, Addison-Wesley, 2018, Chapter 12, Dealing with Inheritance, Pull Up Field
- JetBrains, IntelliJ IDEA documentation, Pull members up, push members down, the first paragraphs
- The Go Programming Language, Effective Go, Embedding
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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag G2P8YE Edit this page History Report an error Markdown