Pull Up Constructor Body

Removes smells
Compare
Symptom
Human

Multiple subclass constructors initialize the same parent fields with the same logic — every `extends Employee` constructor sets `this.name` and `this.salary` identically.

Agent

Multiple subclass constructors initialize the same parent fields with the same logic; the agent verifying constructors must check every subclass for consistency.

Goal
Human

Initialization code repeated across subclass constructors moves into the parent class's constructor and is called via super.

Agent

Shared initialization lives in the parent's constructor and runs via super; subclass constructors hold only their specific setup, and the agent reads one canonical init for parent-state setup.

Before the refactoring

class Manager extends Employee { constructor(n, s) { this.name = n; this.salary = s; } }
class Engineer extends Employee { constructor(n, s) { this.name = n; this.salary = s; } }

After the refactoring

class Employee {
constructor(name, salary) { this.name = name; this.salary = salary; }
}
class Manager extends Employee {}
class Engineer extends Employee {}
Example source: Illustrative example written for this site, not a quotation from any source.
Pressure
Human

Bug fixes in init logic must land in every subclass constructor; new subclasses must remember the pattern; drift between constructors hides bugs.

Agent

Bug fixes in init logic must land in every subclass; the agent must update each consistently or risk silent drift.

Tradeoff
Human

If only some subclasses share the init logic, pulling it up forces the others to opt out — verify the body is genuinely common.

Agent

Subclasses that need different parent-state setup pay the cost of overriding the pulled-up init or passing flags through super; the agent reading those overrides loads both the parent's shared init and the subclass's override to predict what runs.

Relief
Human

One canonical home for parent-state init; new subclasses inherit the setup for free; bug fixes apply uniformly.

Agent

New subclasses inherit the parent's init without re-declaring it; edits to the shared setup land in one constructor and the agent loads one body to verify the change instead of N near-identical subclass constructors.

Trap
Human

Pulling up init logic that only some subclasses share — forces the others to opt out (or override) creating a worse coupling than the original duplication.

Agent

Pulling up init logic only some subclasses need forces the others to override with awkward opt-outs the agent must reason about.