Skip to main content

Command Palette

Search for a command to run...

Code Smell 290 - Refused Bequest

Published
โ€ข3 min readโ€ขView as Markdown
Code Smell 290 - Refused Bequest
M

Iโ€™m a senior software engineer loving clean code, and declarative designs. S.O.L.I.D. and agile methodologies fan.

When you ignore your inheritance, you will have trouble with your parents

TL;DR: Subclasses should honor ALL their parentโ€™s contract.

Problems ๐Ÿ˜”

Solutions ๐Ÿ˜ƒ

  1. Favor composition over inheritance
  2. Don't subclassify for code reuse
  3. Rethink hierarchy
  4. Extract shared logic
  5. Use interfaces
  6. Remove dead code

Refactorings โš™๏ธ

Context ๐Ÿ’ฌ

When you create a subclass, it should use or extend the behavior of its parent.

If it ignores or overrides most of it, you probably force inheritance where it doesnโ€™t belong to reuse code.

This makes the code misleading and hard to maintain.

Sample Code ๐Ÿ“–

Wrong ๐Ÿšซ

class House {
  constructor(address) {
    this.address = address;
  }
  address() {
    return this.address;
  }
  openDoor() {
    console.log("Door opened at " + this.address);
  }
}

class Motorhome extends House {
  constructor() {
    super(null);
  }
  address() {
    return null;
    // This implementation is the same as the parent's
    // and is also a refused bequest
  }
  openDoor() {
    console.log("Motorhome door opened.");
  }
}

Right ๐Ÿ‘‰

class House {
  constructor(address) {
    this.address = address;
  }
  address() {
    return this.address;
  }
  openDoor() {
    console.log("Door opened at " + this.address);
  }
}

class Motorhome {
  // does not inherit from House
  constructor(gps) {
    this.gps = gps;
  }
  openDoor() {
    console.log("Motorhome door opened at " + this.gps.getLocation());
  }
}

Detection ๐Ÿ”

[X] Manual

Look for subclasses that override or ignore most of their parentโ€™s behavior.

You should reconsider the inheritance if a subclass sets parent properties to null or reimplements core methods.

Tags ๐Ÿท๏ธ

  • Inheritance

Level ๐Ÿ”‹

[X] Intermediate

Why the Bijection Is Important ๐Ÿ—บ๏ธ

Your software should reflect real-world relationships.

When you force a subclass that doesnโ€™t logically extend its parent in the Bijection, you mislead developers and introduce maintenance problems.

AI Generation ๐Ÿค–

AI can generate this smell when it defaults to inheritance for reuse instead of composition.

This happens when AI follows generic templates without understanding the context.

AI Detection ๐Ÿฅƒ

AI can detect this smell by analyzing class structures and inheritance trees. However, it struggles with subtle cases where inheritance initially seems valid but breaks expectations.

Try Them! ๐Ÿ› 

Remember: AI Assistants make lots of mistakes

Conclusion โœ”๏ธ

When you design a class hierarchy, you need to make sure that subclasses logically inherit from their parent.

If a subclass refuses some of the behavior, you should rethink your design.

Relations ๐Ÿ‘ฉโ€โค๏ธโ€๐Ÿ’‹โ€๐Ÿ‘จ

More Information ๐Ÿ“•

Refactoring Guru

Code Smells

Disclaimer ๐Ÿ“˜

Code Smells are my opinion.

Credits ๐Ÿ™

Photo by Hanson Lu on Unsplash


Favor object composition over class inheritance.

Erich Gamma


This article is part of the CodeSmell Series.


Code Smells

Part 30 of 50

In this series, we will see several symptoms and situations that make us doubt the quality of our developments. We will present possible solutions. Most are just clues. They are no hard rules.

Up next

Code Smell 289 - Shy Visitor

Don't knock. You are accepted