Composition vs
Inheritance/Mixins
What is mixin?
"Using mixins like this is akin to
cleaning a messy room by dumping
the clutter into six separate junk
drawers and slamming them shut."
What's wrong with mixins?
• They are a violation of the Single Responsibility
Principle
• They are hard to test.
• Where the called code lives?
• http://blog.8thlight.com/eric-meyer/2012/11/16/
composition-over-mixins.html
Composition benefits
• Fits better for deep hierarchy
• You call methods on instances instead of global
namespace ("Engine.new.start" instead of
"start_engine")
• Use only what you need, easier to share code, no
method name conflicts
Replace Inheritance with
Composition
• http://blog.codeclimate.com/blog/2012/10/17/7-
ways-to-decompose-fat-activerecord-models/
• http://blog.8thlight.com/eric-meyer/2012/11/16/
composition-over-mixins.html
• http://reefpoints.dockyard.com/2013/10/01/design-
patterns-composite-pattern.html
• Ruby Science book
THANKS!

Composition over inheritance