PDA

View Full Version : How much inheritance do you use?



Michiel
30th July 2006, 23:02
I sometimes read about huge inheritance trees. Like in the book [Refactoring]. Some of the big refactorings include teasing apart large inheritance trees that have gotten out of hand. However, I find myself using class composition a lot more than class inheritance. I rarely find myself with an inheritance tree higher than 2, which can hardly be called a tree at all. Some people call my style 'flat', but I consider my code elegant and modular.

What about you? How much inheritance do you use? When is it useful to have a big inheritance tree?

Oh, I'm not counting frameworks like QT. I understand the need for extensive inheritance there, because you can never predict what the user will need.

high_flyer
31st July 2006, 14:37
I understand the need for extensive inheritance there, because you can never predict what the user will need.
I think you misunderstood the use of inheritance.
It has nothing to do with the users needs.
Its about encapsulation and reusing of code and polymorphism.
The question it self is a bit strange - inheritance is a tool, and you should use it for what its meant for, there is no "large is bad, small is good" or vice versa in this regard.
If your code a zoo, with 10000 animal species, then you are bound to have a large inheritance tree, since all animals share some attributes and actions.

wysota
31st July 2006, 16:03
And remember you should count not only the size of the tree "upwards" but also "sidewards". You can have a class which has a hundred direct subclasses and the height of the tree will still be 2.

Michiel
31st July 2006, 22:29
Sure. But there are problems that can be solved by either inheritance or composition. I prefer the latter in most cases. Of course neither method is 'right' or 'wrong'. It's a matter of style, I think.

I have lots of inheritance trees with a large width. But it's really the height I am talking about. (When I say 'huge', I mean 'high'. Sorry, my mistake.)

And the inheritance in QT certainly has something to do with the users needs. If you wish, you can create a textedit widget with some small modifications. Or only use the scrollarea. Or just use widget class and work from there. (Just to name an example.) If QT were only created only for a single project, it would no longer be a framework for public use and a lot of the inheritance tree could have been collapsed.

You know, not one design pattern (from the book [Design Patterns], at least) uses an inheritance tree higher than 2. With the possible exception of the command pattern with undoable commands. That is, incidentally, the only tree higher than 2 I've ever used.

I was just curious. It's possible that my question is weird or hard to answer. Let me rephrase. In practice, how high is your avarage inheritance tree?

Kumosan
1st August 2006, 10:07
I was just curious. It's possible that my question is weird or hard to answer. Let me rephrase. In practice, how high is your avarage inheritance tree?

One. I rarely need multiple inheritance. And in the very rare cases where I do need it, it usually is only a pure virtual "interface'.

high_flyer
1st August 2006, 10:27
I prefer the latter in most cases.
I use it when ever it is needed.

In practice, how high is your avarage inheritance tree?
The average in my case is derived from the average kind of design I use, which is derived from the problems I need to solve with the applications I code, so I never did any averaging on it, and I don't tend to prefer one design paradigm on another - its problem specific.
Just like you would not use a screw driver to nail a nail in, I don't see the point in preferring a design paradigm on its own merits with out considering the problem that needs to be solved at hand.

Michiel
1st August 2006, 20:48
Oh, come on. :) There's more than one solution to every problem. Every programmer develops his/her own style.

Kumosan, I wasn't talking about multiple inheritance. I was talking about the number of 'generations' in your inheritance tree. So if you ever use inheritance, the tree is at least 2 high.

Kumosan
1st August 2006, 23:13
Kumosan, I wasn't talking about multiple inheritance. I was talking about the number of 'generations' in your inheritance tree. So if you ever use inheritance, the tree is at least 2 high.

Ok, sloppy reading on my part. Falsely assumed multiple inheritence, which usally is the theme. ;)

Nevertheless, never counted, but usually it is 2. Cannot remember if I ever needed more. If I search my code I suppose I might find somewhere a 3rd gen., but probably not more. Like high_flyer I use what I think it is most appropriate for the given problem. Do you know a free program which produces code metrics like in your question? If so I could give you an exact answer. :D

Michiel
1st August 2006, 23:29
Neh. I don't know anything of the kind. :)