Re: Maximum Accepted Layers/Levels of inheritance
On Sep 12, 2:43 am, Phlip <phlip2...@gmail.com> wrote:
[Google won't show me the original posting you are
responding to, so I'll comment it here.]
Daniel T. wrote:
Make it as deep as you want.
Make it as deep as you have to. In many ways, the shallower the
better, but of course, there are more important criteria which
may push the level up a little. I'd be suspicious if, in an
application, more than about 5% of the hierarchies were deeper
than 3, and I'd probably at least glance at those hierarchies,
just to make sure that the depth was justified.
However, I think that all base classes in
C++ should contain only pure-virtual functions and no data (i.e.,
interface classes.)
Which ignores some important patterns, like the template method
pattern. Also, of course, in C++, it's fairly rare for public
functions to be virtual; the usual idiom is to use public
functions to specify the interface, including the contract (pre-
and post-conditions), and have them forward to private virtual
functions for the implementation details.
Isn't this putting the cart before the horse?
Totally agreed. We define a solution, without having specified
a problem.
What if, for some given real-code situation, a few
implementations should be in the base class. Yes, that's just
a convenience. Moving them down, at refactor time, should be
trivial. So just leave them there.
If the contract says that they must have a specific interface,
then yes, they belong in the base class. If it's just a
convenience, it's often preferable to put them in an
intermediate class, if only to reduce compilation dependencies
(but I find that it helps readability as well). At any case,
it's a judgement call.
The other arch-enemy here is junior
programmers behaving as if designs must never be changed...
On large projects, a junior programmer doesn't have the right to
change the design himself. At least not if the change involves
changes in the interface, which breaks other peoples code. (But
I agree with the thrust of your comment. Just replace "junior
programmers" with "pointy-haired managers".)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34