Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deep class inheritance hierarchy -- bad idea?

hoping a grandmaster can shed some light. Very high overview is that I am no beginner to coding, but still new to OOP. This set of message classes is at the heart of a large simulation application we're writing, and I don't want to do it stupidly--this interface cuts the application in half, from sequencer to executer and vice-versa.

My question is whether or not it's a bad idea to have an inheritance hierarchy this deep (image is not yet fleshed out, might go 5 or 6 deep in the end). This is as opposed to having some of the child classes just have a directed association to their parent class, instead of inheriting.

I've read that a deep inheritance hierarchy is not a good idea, and that if a child class is inheriting simply to have the parent's data, then you should simply include the parent as data in the child, but I'm having a hard time wrapping my head around why. What bad thing is going to happen to us if I decided to make an inheritance hierarchy 7-deep or something like that? Clearly there's a small performance hit, and changing things at the top of the hierarchy is going to have huge ripples throughout the app, but other than that I don't see an issue. Aside, I care little about minor differences in performance.

Class Hierarchy

(bonus question: Is there an off-the-shelf package that handles this kind of stuff? We have most of the low level physical simulations handled, but the sequencing program we're going to have to write. I just have this suspicion that what I've laid out is very similar to what about 10,000 simulation developers before me did.)

(bonus question #2: any masters of both simulation systems and OOP programming, that would not hate living in Los Angeles? We're hiring.)

like image 603
Spatula City Avatar asked Jun 15 '12 19:06

Spatula City


1 Answers

that if a child class is inheriting simply to have the parent's data

This is a bad idea. There's this understanding that you define base classes as the most generic of contracts that a set of (concrete) classes are going to honor. This typically means that your contract is about behavior and not implementation.

What bad thing is going to happen to us if I decided to make an inheritance hierarchy 7-deep or something like that?

The major issues here are mundane:

  • Fragile base classes (changes to base are a nightmare for the derived)
  • Increased coupling (with too many base classes comes tight coupling)
  • Encapsulation weakens
  • Testing issues (leaf level overridden methods can't just be tested to reproduce end-user behavior correctly always due to multiple chained calls here and there)
  • Maintenance (comes from strong coupling)

(You many want to peruse this paper on Why Ada isn't popular, particularly, Item 6, para 6.)

Is there an off-the-shelf package that handles this kind of stuff?

I'm not sure what you are looking for, but if you're looking for an automated hierarchy simplifier then I don't know of any. Also if such a package exists it'll be highly dependent on your language of choice and you haven't mentioned one.

Note that most of the times such issues can be resolved by looking at alternatives like aggregation or traits or dependency injection or whatever. These are design time issues and are typically (IMO) best ironed out on a whiteboard than with a compiler and millions of LOC.

like image 83
dirkgently Avatar answered Oct 02 '22 00:10

dirkgently