Results 1 to 9 of 9

Thread: How much inheritance do you use?

  1. #1
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default How much inheritance do you use?

    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.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How much inheritance do you use?

    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How much inheritance do you use?

    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.

  4. #4
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How much inheritance do you use?

    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?
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  5. #5
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: How much inheritance do you use?

    Quote Originally Posted by Michiel
    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'.

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How much inheritance do you use?

    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.

  7. #7
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How much inheritance do you use?

    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.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  8. #8
    Join Date
    Aug 2006
    Posts
    221
    Thanks
    3
    Thanked 29 Times in 19 Posts

    Default Re: How much inheritance do you use?

    Quote Originally Posted by Michiel
    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.

  9. #9
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How much inheritance do you use?

    Neh. I don't know anything of the kind.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

Similar Threads

  1. Inheritance and QpaintEvent
    By djoul in forum Qt Programming
    Replies: 22
    Last Post: 5th July 2006, 13:56
  2. The single Inheritance Approach
    By raphaelf in forum Newbie
    Replies: 2
    Last Post: 2nd June 2006, 08:51
  3. Multiple Inheritance & Qt
    By kefeng.chen in forum Qt Programming
    Replies: 8
    Last Post: 21st March 2006, 18:37
  4. Multiple inheritance & Qt
    By dublet in forum Qt Programming
    Replies: 11
    Last Post: 8th March 2006, 08:12
  5. Multiple Inheritance
    By sunil.thaha in forum General Programming
    Replies: 4
    Last Post: 21st February 2006, 04:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.