Programming Search⌘K

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

  1. 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.
  2. 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.
  3. Change each reference to the removed class to the class that stays. Test.
  4. 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);
Output
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
PythonGitHub
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)
Output
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)
}
Output
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

  1. Martin Fowler, Catalog of Refactorings, Collapse Hierarchy, the entry, the Employee and Salesman sketch
  2. Martin Fowler, Catalog of Refactorings, index, the entry collapseHierarchy, its books and tags
  3. 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.

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