Code Smell 290 - Refused Bequest

Code Smell 290 - Refused Bequest

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 subclass 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

Without Proper Instructions

With Specific Instructions

Conclusion ??

When you design a class hierarchy, you must ensure that subclasses logically inherit from their parent.

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

Relations ????????

Code Smell 11 - Subclassification for Code Reuse

Code Smell 58 - Yo-yo Problem

Code Smell 43 - Concrete Classes Subclassified

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.



要查看或添加评论,请登录

Maximiliano Contieri的更多文章

  • Refactoring 001 - Remove Setters

    Refactoring 001 - Remove Setters

    Setters violate immutability and add accidental coupling TL;DR: Make your attributes private to favor mutability…

  • Code Smell 292 - Missing Return

    Code Smell 292 - Missing Return

    When your code loses its way TL;DR: Missing return statements cause unexpected behavior. Problems ?? Silent failures…

  • Code Smell 291 - Mutable Keys

    Code Smell 291 - Mutable Keys

    Changing Keys, Losing Values TL;DR: When you use mutable objects as keys in hashed collections, changing them breaks…

    2 条评论
  • Refactoring 023 - Replace Inheritance with Delegation

    Refactoring 023 - Replace Inheritance with Delegation

    Transform your rigid inheritance into flexible delegations TL;DR: Replace restrictive inheritance hierarchies with…

  • 16 Simple Tips to Boost Your Productivity x10

    16 Simple Tips to Boost Your Productivity x10

    TL;DR: More productivity tips These easy-to-follow tips will improve your output dramatically. 1.

  • Code Smell 289 - Shy Visitor

    Code Smell 289 - Shy Visitor

    Don't knock. You are accepted TL;DR: Avoid combining the Visitor pattern with instanceof checks.

    1 条评论
  • Code Smell 288 - Unthrown Exceptions

    Code Smell 288 - Unthrown Exceptions

    TL;DR: Creating a new exception without throwing it leads to silent failures. When You Forget to Throw, Your Code Will…

    1 条评论
  • Code Smell 287 - Unused Local Assignment

    Code Smell 287 - Unused Local Assignment

    Are you using the returned value? TL;DR: Avoid assigning values you never use. Problems Dead code Unclear intent…

  • Refactoring 022 - Extract Common Ancestor

    Refactoring 022 - Extract Common Ancestor

    Make your class hierarchy clear and flexible TL;DR: Extract a common abstract class to mimic real-world structure…

  • Explain in 5 Levels of Difficulty: Quantum Computing

    Explain in 5 Levels of Difficulty: Quantum Computing

    Back to the Future: Revisiting Quantum Computing 25 years later More than 25 years ago, circa 1999, I authored an…

其他会员也浏览了