Bad Smells: Inheritance


The relationship between a class and its subclass usually starts out simple but becomes more complicated over time. A subclass often depends on its generalization more closely than an extraneous class, but this may be too much.

The key is to decide between what a class is and what a class has. The structure of a class usually begins with inheritance and moves more toward composition over time.

Bad smells emerging from misuse of inheritance are:

Refused Bequest

Symptoms

To do

*Leave it as it is, if it’s not confusing.

Rewards

Contraindications

Sometimes a case of Refused Bequest is used to prevent an explosion of new types.

Inappropiate Intimacy (Subclass Form)

Symptoms

A class accesses internal parts that should be private from their generalization. If this occurs between separate classes, it is known as General Form.

To do

Rewards

Lazy Class

Symptoms

A class barely has any behavior. Its generalizations, subclasses, or clients do all the work apparently associated with it, and there is not enough behavior in the class to justify its existence.

To do

Rewards

Contraindications

Sometimes a Lazy Class exists to communicate an intention. You must find the balance between communication and simplicity.

2019-03-20
Written by Samuel de Vega.
Tags