Is code smelly having empty classes in the middle of a class hierarchy?

I sometimes end up with a class hierarchy, where I have an abstract base class with some common functionalities and several implementation classes that fall into two (rarely more) groups, which I want to consider differently in some cases. An example is the abstract tree node and various implementations of branches and leaves, where I want to highlight branches and leaves at some point.

These intermediate classes are then used only for "is-a" statements in flow control, and they do not contain any code, although I had cases when they "grew up" in some code later.

Does that sound smelly to you? In my example on a tree, one alternative would be to add abstract methods isLeaf()/ isBranch()to the base class and implement them on intermediate classes, but for me this did not seem to be better, d means that there can be several classes at once.

+5
source share
6 answers

In general, empty classes are the smell of code.

I agree that your methods isLeafor isBranchare the right alternative. They add information about objects , which is useful. (This is because in a superclass you cannot express that subclasses are a "leaf or branch").


.

... LEAF BRANCH.

+1

"is-a" , switch/case. OO .

+4

, .

+2

Yup, , - , . YAGNI ( ) - , .

, , ?

, , , ?

+2

A class that does not contain any code is definitely a code smell ....

0
source

It seems to me good if you add new features later.

But if not, an enumeration is usually used here.

- Change

Although I have to agree with Ber that you should not use 'is-a' at all.

0
source

All Articles