This is a companion discussion topic for the original entry at https://thoughtbot.com/upcase/videos/composition-over-inheritance
Great episode. 'Prefer Composition over Inheritance` is something that I hear often but usually neglect to apply it, I guess because my brain is wired with a more āinheritanceā-like style of thinking. Iāll need to watch this a few more times to help it sink in.
A couple of questions:
- Are there any situations where traditional inheritance still makes sense? Or is it better to always go with composition to provide for more flexibility as code grows over time?
- And what about Ruby modules, should they generally treated as a code smell? How do you evaluate whether they are a good choice or not?
@andyw8 Iāve seen Sandi Metz (I recommend watching that whole video if you havenāt already) say that inheritance is good when a subclass basically overrides some methods and very little else, kind of Interface Segregation Principle related - as in donāt inherit from a super class just to get a tiny subset of behaviour thatās not core to the purpose of that class, prefer composition in that case.
By coincidence just I watched that just a few days ago. It was a very useful companion to the ideas presented in the episode.
Hi,
Great discussion. I feel like there was a lot of agreement that we should almost always use composition over inheritance. Having said that I wanted to challenge that there are cases where one would use inheritance/include.
For example when creating an API wrapper it seems to be common practice to include all functionality required in a client via includes. I thought maybe there is a way to leverage composition, but I havenāt been able to come up or find any examples. I also checked what APIs thoughtbot has written and found that they have worked on the Yam Gem which has been contributed to by thoughtbot and I wanted to find out why is it ok to use includes in the client library? This seems to contradict what is said in the discussion.
Any help would be appreciated.
I guess you mean yam/client.rb at 11eb501363b4dc1e3de4117066e8c1e2f2842503 Ā· yammer/yam Ā· GitHub ?
Yes, sorry I thought I included that link. That is what I am referring to.
I really enjoyed this video. Thank you @r00k and @jferris for putting together a great explanation of composition and why it supports more flexible and reliable systems than inheritance.
A lot Rails consists of inheritance. I was wondering if either of you, @r00k or @jferris, could comment on software like, Rails. Here are some questions I have for you both:
- Do you two think that Railsās use of inheritance is a poor design choice?
- Or is there something about the type of software that Rails is ā a web framework ā which makes it a strong and practical candidate for inheritance?
- If Railsās use of inheritance is a good design choice, then do we have a use case where we choose Inheritance Over Composition?
Also, the above questions are not just for Ben and Joe. Feel free to chime in anyone and everyone.
Do you think Composition over inheritance contradicts Convention over configuration?