When does inherience trees in your opinions get too deep? (C#/C++)

6    12 Jul 2015 13:07 by u/Hjorthenify

Title says it all. Microsoft says above 4 for C# but what are your opinions / experience when it comes to depth of inherience?

https://msdn.microsoft.com/en-us/library/ms182213.aspx

3 comments

0

Around 3-4 sounds about right for an application, anything beyond that could probably be refactored. The exception to that would be if you're designing a framework, where it could be very useful to break that rule to provide a cleaner API.

0

This. There are some problems with object-oriented as a paradigm, and composition is an interesting alternative that has a lot of benefits if your type system can back it up. No individual paradigm makes 'the right' compromises in every situation--even as I'm driving toward protocol-based composition as my primary tool in Swift, I still am very glad to have good ol' classes around for a handful of situations.

I think that you should consider that, anywhere you have an inheritance tree deeper than three or four, you should ask yourself if you're encapsulating your variation correctly. Ideally, if you have fourth and fifth derivative objects, then they should spend most of their lives being treated as anonymous concrete instances of some superclass at the third or fourth level.

0

Deep inheritance usually means you are inheriting along multiple "axes" of variability. In a language without interfaces or multiple inheritance (and sometimes even in those) you should model multi-dimensional objects with composition.

There are exceptions of course, especially if you are making an extensible framework.