During most of my time working through Object-Orientation, I would endlessly find myself writing methods in various classes. Of course, as a beginner at FlatIron, I spend most of my time shifting from concept to concept and as I learn more, I have begun to see a pattern of writing the same class methods over and over again. But why? As a beginner, it is very useful to practice writing out these methods and witness yourself becoming fluent in building classes. However, once you are familiar with the process you can’t help but think about DRY, the Don’t Repeat Yourself principle. This blog is about one way that I have learnt to be more ‘DRY’ : implementing Module Inheritance
What is Module Inheritance?
First of all, Inheritance is the process of being able to create a new class based on an existing class that inherits the ‘parent’ class methods. Think of it as a family tree and a person as a class. A Module is a collection of methods and constants that can be used by any class as an instance or class method. There are two ways in accessing a module and that’s ‘include’ and ‘extend’. By ‘including’ a module in a class we can use the module methods on any instance of the class itself. By ‘extending’ a module into a class, we can use its methods on the class itself. If you find yourself working on multiple classes and are finding yourself repeating multiple methods, a module is a great way to abstract that code into one place while being able to access it across classes.
Let's get to it:
Here, we have two simple classes called ‘Dancer’ and ‘Kid’. As you can see in both, they carry methods that are identical to each other. What if we wanted an app that models the environment of a dance performance. We would, of course, have professional dancers who perform and simultaneously have a kid who sees that performance and wishes to practice the same moves. We find ourselves repeating multiple methods. This is a perfect scenario where we would wish to create a module. By creating a module that both these classes can call upon for the same method, we essentially minimalize the amount of rewritten methods to be held in one place.
Ta-da! we have a module!:
I’m sure you can spot that within this module ‘FancyDance’ we have an additional two modules called ‘InstanceMethods’ that hold the methods previously seen in the classes and a ‘ClassMethods’ module that holds a method that we will refer back to later. Congratulations, we now have two classes that now look like this:
As you can see, the class will no longer have the methods. Perfect! Less code the better but we still need a reference point so the class can access those methods. And here comes Include and Extend. By having ‘include’ we allow the methods in the module under ‘InstanceMethods’ to be used on any instance of the class itself. While the ‘extend’ accesses the ‘ClassMethods’ method to be called on the whole class. Now to test our plan:
There you go! We see that both our instance method and class method is working perfectly. We no longer need to repeat ourselves and can call on any of the collection of methods in our modules to either class. This is a simple way of expressing how to use modules in your program but the possibilities and scope are endless and remember, choose the solutions that best fit you to work on DRY.
Gif of the week: