I'm not entirely sure what I am basing this theory on but I believe that favouring composition over inheritance is when a programmer/developer is maturing into a better programmer who creates code that is easier to maintain and extend, along with solving the problem you are writing the code for the two main goals of my coding.



I wrote an article about why you should use Composition instead of inheritance and as always the comments for this post were very interesting.



It's just a personal opinion of course but initially I use to think first of using inheritance because it seems easy and you can use code in the base class in all the classes that inherit this class. I think this is why people favour inheritance.



I think the next step of evolution as a programmer is using interfaces as a way to protect against change in the future.



The next stage that perhaps I am at (what I like to think, my work colleagues may disagree) is that you start to use composition. I have stopped thinking that the best code is the least amount of code written, which was one of the reason I use to use inheritance. I now think of good code as code that has loose coupling and that does one thing i.e has high cohesion. This type of code can easily be reused and change in any part of the code will hopefully affect a small part of the code base. Code should be DRY and many other things but I am not talking about that kind of code intricacies but sort of looking at the code at a more holistic view.



I believe that favouring composition is way to achieve the code above. I would like to get it clear that I am not against using inheritance and there is a time and place for using it when it's the best option etc. What I am trying to say is that there was a stage where I would try to use inheritance a lot but then over time I have stopped doing this and the reason for this is because I think my coding has matured and the goals of code I am trying to create have changed.



I'm not really sure when the change of thought process occurred but I will now spend a bit more time creating code like the above to try and manage the complexity of the code and the effect of change on the code. Of course this can also be abused with the creation of a million to many classes, as with everything there is a balance to achieved but more classes maybe better than too few because at least change is more likely to be isolated then.



I would like to know what other people think about this and what's the next stage of development of a programmer







If you like this blog or and fancy something a bit less technical with some laughing thrown in then check out my other blog Amusing IT Stories. Which is a blog about funny and amusing stories from the IT environment and the office. It is a mix of news, office humour, IT stories, links, cartoons and anything that I find funny





