🧀
Smells to Refactoring
English
English
  • Introduction
  • Quick Reference Guide
  • Bloaters
    • Long Method
    • Large Class
    • Primitive Obsession
    • Long Parameter List
    • Data Clumps
  • Tool Abusers
    • Repeated Conditional Complexity
    • Refused Bequest
    • Alternative Classes with Different Interfaces
    • Temporary Field
  • Change Preventers
    • Divergent Change
    • Shotgun Surgery
    • Parallel Inheritance Hierarchies
    • Combinatorial Explosion
  • Couplers
    • Feature Envy
    • Inappropriate Intimacy
    • Message Chains
    • Middle Man
    • Indecent Exposure
  • Dispensables
    • Comment
    • Data Class
    • Dead Code
    • Duplicated Code
    • Lazy Class
    • Speculative Generality
    • Oddball Solution
  • Incomplete Library Class
  • 😎About me
Powered by GitBook
On this page
  • Sign of Smell
  • Reason of Smell
  • Refactoring Recipe
  1. Tool Abusers

Refused Bequest

When a class unnecessarily inherits code from its parents.

Sign of Smell

The subclass does not use most of the functionalities of its parents or overrides them with empty or meaningless implementations. In fact, the SuperClass and SubClass are quite different from each other. Rather than tolerating inheritance, you tend to write code that refuses the "bequest".

Reason of Smell

  • Violation of Liskov Substitution Principle (LSP): The Liskov Substitution Principle states that objects of a superclass should be replaceable with objects of a subclass without affecting the correctness of the program. When a subclass refuses inherited methods, it violates the LSP, making it difficult to use the subclass as a true substitute for the parent class.

  • Misleading Design: When a subclass inherits methods from a parent class but doesn't use them or provides empty implementations, it can confuse developers who expect the inherited methods to work as intended.

  • Incomplete Behavior: Inherited methods are expected to provide certain behavior or functionality. If a subclass refuses the inherited methods, it might lack the necessary behavior or cause unexpected errors when those methods are invoked.

  • Maintenance Challenges: The subclass might need to be updated in the future due to changes in the parent class or to implement the missing behavior. This can lead to inconsistencies and increased maintenance effort.

  • Wasted Resources: Inheriting methods that are not used or are overridden with no-op implementations wastes memory, and development effort, and can complicate the understanding of the code.

  • Poor Code Reusability: The main purpose of inheritance is to promote code reusability. When a subclass refuses bequest, the reusability of the inherited methods is compromised.

Refactoring Recipe

To address this code smell, we can try the following refactoring skills:

  • Push Down Field

  • Push Down Method

  • Replace Inheritance with Delegation

Reference

PreviousRepeated Conditional ComplexityNextAlternative Classes with Different Interfaces

Last updated 1 year ago

https://refactoring.guru/smells/refused-bequest
https://ithelp.ithome.com.tw/articles/10212037
https://refactoring.guru/push-down-field
https://refactoring.guru/replace-inheritance-with-delegation