Pure methods are really amazing. There are three reasons to use pure methods whenever possible:
1. Easier to test
2. Reduce side effects
3. Easier to reuse
Let’s start with what a pure method is. A pure method is a method that does not use ANY instance or class data. To allow a pure method to do anything, we must pass it arguments.
Here’s a very basic example of two classes that have a simple method. One is pure and one is not.
class Dirty def initialize(num1, num2) @num1 = num1 @num2 = num2 end #Add the two instance variables def dirty_adder @num1 + @num2 end end class Pure def initialize end #Add two arguments def pure_adder(first_number, second_number) first_number + second_number end end
These classes add two numbers. One uses the instances variables assigned at initialization, the other requires the arguments to be passed in. Let’s take a look at my 3 reasons to favor the pure method.
1. Easier to test.
To test the dirty method, we need to do this:
dirty = Dirty.new(5,4) => #<Dirty:0x007f9aa2698f68 @num1=5, @num2=4> dirty.dirty_adder => 9 pure = Pure.new => #<Pure:0x007f9aa26e11a0> pure.pure_adder(4,6) => 10 pure.pure_adder("Cat", 5) TypeError: no implicit conversion of Fixnum into String
Either work fine, but when we actually test the method, one method gives us more information. What if num1 was changed into a string “cat” in dirty_adder? We would need to follow that variable around the code until figure out where the error in assignment occurs. With pure_adder, we can make sure it works and never look to that method again.
On a very practical level, it is very easy to copy a method in to pry or IRB and messier to copy an entire class. Which leads to point #2.
Side effects are a concept in computer science. Side effects in methods can be thought of as roots going from the method into the class. Every time a method touches an instance variable, there is an opportunity for side effects to arise. If the method is removed or changed, it make have side effects on other methods. Side effects can change values, methods, and increase code inter dependency. Code inter dependency becomes a large problem. Changing a part of one method could invalidate the results of others. Side Effects
Lastly, let’s look at reason #3: Easier to reuse
#Add two arguments def pure_adder(first_number, second_number) first_number + second_number end
This code has no dependency on any part of our class. This method could be added to any class with out causing problems with existing code. Once I write a truly beautiful pure method, I get to reuse that over and over again with out fear across multiple classes or projects.
This does not mean you should not use instance variables in methods. There are many times when pure methods are not an option. The idea I am trying to impart, is if you can write a pure method with out affecting your functionality, do it. It will be easier to test, less likely to cause unpredictable problems, and reduce work going forward.
I learned about pure methods from this presentation. It’s done largely in haskel and python but is pretty easy to follow.