The DRY Method

Arun M
Rube’s Guide
Published in
3 min readJul 2, 2017
See how dry these lemons are? Your code should be drier…or wetter. It depends.

Let’s talk about what makes your code redundant.

DRY stands for Don’t Repeat Yourself. It is a way of combining like methods that can be referred to from the original source. There are some rules dictating what can and cannot be done with DRY. We will examine those rules and look at one of the types of DRY methods that you will come across. The other method will be tackled in another blog post.

DRY Method With Inheritance

Inheritance refers to a relationship between two classes. Let’s illustrate this. Say you have a class for Car, and another class for Bike. They both share methods:

class Cardef initialize
@speed = 0
@direction = ‘north’
end
def brake
@speed = 0
end
def accelerate
@speed += 10
end
def turn(new_direction)
@direction = new_direction
end
def honk_horn
puts "Beeeeeeep!"
end
end
class Bike
def initialize
@speed = 0
@direction = ‘north’
end
def brake
@speed = 0
end
def accelerate
@speed += 10
end
def turn(new_direction)
@direction = new_direction
end
def ring_bell
puts “Ring ring!”
end
end

Why should this code repeat in both the Car and Bike class? They share the same information, so there should be a way to represent this data once and let the Car and Bike classes pull from it.

This is accomplished by creating a new parent class and dumping in all of the common elements between the Car and the Bike classes.

Let’s create a new class called Vehicles. Vehicles is a good Class name because we need a name that will broadly describe the basic functions of both a car and a bike.

class Vehiclesdef brake
@speed = 0
end
def accelerate
@speed += 10
end
def turn(new_direction)
@direction = new_direction
end
end

What we have done here is take the common elements from the Car and Bike classes and put them into a new class called Vehicles.

Our Car Class should now look like this:

class Car < Vehicle
def initialize(speed,direction,fuel,make,model)
super() # Force super to not take any arguments in
@speed = 0
@direction = ‘north’
@fuel = fuel
@make = make
@model = model
enddef honk_horn
puts “Beeeeeeep!”
end
end

The only method we are defining is ‘honk_horn’ which is a unique function of a car. Now let’s look at our Bike Class:

class Bike < Vehicle
def initialize(speed,direction,type,weight)
super()
@speed = 0
@direction = ‘north’
@type = type
@weight = weight
end
def ring_bell
puts “Ring ring!”
end
end

The lone method here is ‘ring_bell’ which is another function specific to bikes.

Please notice how we are now defining the car and bike classes:

class Bike < Vehicleclass Car < Vehicle

The syntax has now changed because we need a way to call all of the methods that we moved into the Vehicle class. The ‘<’ sign after the class name means that we are about to call a parent class. It is saying that the newly defined class will include all the methods of the parent class and any new classes that are defined in the child class.

Notice that we have left the initialize method in each class even though they are identical. Why didn’t we move this into our Vehicles class? The reason is that the type of information in the initialize method might not always equal each other like it does in this example. Cars and bikes have different speeds and they could be moving in different directions. Leaving them in the child classes allows for this possible variance.

DRY has many benefits and varying opinions both positive and negative. The immediate positives are that you can shrink your code significantly. It also becomes a life saver as you deal with several classes and hundreds of lines of code. The downside is over DRY-ing. That is, combining elements that might seem similar but in fact could have varying info at any moment, like discussed in the initialize method. It will be up to you to use your discretion and DRY as needed. Good luck!

--

--