Techniquecore
Collapse hierarchy
Merge a subclass into its superclass, or the superclass into the subclass, when the two are no longer different enough to be two classes.
- Family
- refactoring
- Intent
- Remove a level of a hierarchy that adds nothing.
Purpose
A hierarchy changes as the code changes. Features move up and down, and after a time a subclass can be almost the same as its superclass. Then the two classes are one idea in two places, and a reader must look at both to understand either.
Collapse hierarchy merges the two. The catalogue card has class Employee and class Salesman extends Employee. After the refactoring, only Employee is left.1
Steps
- Choose which class to remove. Keep the one whose name fits the merged class better. If neither name fits, pick one and rename it afterwards.
- Move all the features of the class to remove into the one to keep, with Pull Up Method, Pull Up Field, Push Down Method and Push Down Field.
- Change each reference to the removed class to the class that stays. Test.
- Remove the empty class. Test.
Example
After earlier changes, Salesman only adds a region with a default value. The region moves into Employee, and Salesman goes.
import assert from "node:assert/strict";
class EmployeeBefore { constructor(name) { this._name = name; } get name() { return this._name; } }
class SalesmanBefore extends EmployeeBefore {
constructor(name, region = "none") { super(name); this._region = region; }
get region() { return this._region; }
}
class Employee {
constructor(name, region = "none") { this._name = name; this._region = region; }
get name() { return this._name; }
get region() { return this._region; }
}
const before = new SalesmanBefore("Ana", "north");
const after = new Employee("Ana", "north");
assert.equal(after.region, before.region);
assert.equal(after.name, before.name);
console.log(after.name, after.region);
Ana north
| Language | What the example shows |
|---|---|
| JavaScript | Salesman merged into Employee, as above |
| Python | the same merge, with a dataclass that has a default region |
| Go | the same merge; the struct that embedded Employee is removed, and the field moves into Employee |
from dataclasses import dataclass
@dataclass
class Employee:
name: str
region: str = "none"
ana = Employee("Ana", "north")
assert Employee("Bo").region == "none"
print(ana.name, ana.region)
Ana north
package main
import "fmt"
// Salesman, a struct that embedded Employee to add Region, is gone.
type Employee struct {
Name string
Region string
}
func NewEmployee(name string) Employee { return Employee{Name: name, Region: "none"} }
func main() {
ana := NewEmployee("Ana")
ana.Region = "north"
fmt.Println(ana.Name, ana.Region)
}
Ana north
When to use
- A subclass that adds almost nothing. See Lazy element.
- A hierarchy made for a need that did not come. See Speculative generality.
- After many moves. When most features have moved up or down, check if each level of the hierarchy still has a reason to exist.
Pitfalls
Other subclasses. If the superclass has other subclasses, merging the superclass into one of them is not possible. Merge the subclass up instead.
Constant differences. If several subclasses differ only in data, use Remove Subclass, which replaces them with a field.
Public types. If code outside your control uses the class that you remove, keep the name, for example as an alias, until that code changes.
Tools that do it
No IDE has one action for this refactoring under this name. The moves of the features use Pull Members Up and Push Members Down, and the IDE can then find the remaining references to the empty class.
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
- Martin Fowler, Catalog of Refactorings, Collapse Hierarchy, the entry, the Employee and Salesman sketch
- Martin Fowler, Catalog of Refactorings, index, the entry collapseHierarchy, its books and tags
- Martin Fowler, Refactoring, Improving the Design of Existing Code, second edition, Addison-Wesley, 2018, Chapter 12, Dealing with Inheritance, Collapse Hierarchy
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 0VSVGF Edit this page History Report an error Markdown