Code Refactoring with Parameterization

Reducing complexity by refactoring with parameterization.

Update (13 July 2017): I fix the wrong weight average logic in the code and also provide a working example that you could run on your machine. :awesome:

There are multiple occasion that I came across an ‘almost similar’ chunk of codes all over the code base. The problem with this kind of code smell is, once you want to change just a tiny bit of that particular function, you need to find all the implementation you did and change all of them which is not a good idea.


Duplicate code is a major cause of unmaintainable scripts.



Brian Marick @ Everyday Scripting with Ruby

The technique that I often use and find it’s super easy to do is parameterization. Today I’ll explain how that works by a nice little example.

Finding weight average

There are reports that I need to find a weight average of a gross profit field and they are going to be weighted by sale revenue. Here is what I came up with. (in Ruby)

def weight_avg_gross_profit(reports)
  # find total weight, sum of sale_revenue
  total_weight = reports.reduce(0) { |sum, r| sum + r.sale_revenue }

  # find sum products
  sum_prod = reports.reduce do |sum, r|
    sum + (r.percent_gross_profit * r.sale_revenue)
  end
  
  # simple weight average formula, (w1*x1 + w2*x2) / (w1 + w2)
  sum_prod / total_weight
end

Simple stuff right? Now we need another method that does this exact same thing but for another field called cogs. The easiest thing here is to just duplicate the method and change what’s need to be changed.

def weight_avg_gross_profit(reports)
  # find total weight, sum of sale_revenue
  total_weight = reports.reduce(0) { |sum, r| sum + r.sale_revenue }

  # find sum products
  sum_prod = reports.reduce(0) do |sum, r|
    sum + (r.percent_gross_profit * r.sale_revenue)
  end
  
  # simple weight average formula, (w1*x1 + w2*x2) / (w1 + w2)
  sum_prod / total_weight
end


def weight_avg_gross_profit(reports)
  # find total weight, sum of sale_revenue
  total_weight = reports.reduce(0) { |sum, r| sum + r.sale_revenue }

  # find sum products
  sum_prod = reports.reduce(0) do |sum, r|
    sum + (r.cogs * r.sale_revenue)
  end
  
  # simple weight average formula, (w1*x1 + w2*x2) / (w1 + w2)
  sum_prod / total_weight
end


Make it work, then make it right ❤



We got what we want right? Cool but what if we need weight average of other stuff? What if we need to change the weight from sale_revenue to something else? Creating new method that shares this logic would lead us to the problem I was talking about earlier., the duplication problem.

Parameterization in action

Now what do we see, these two methods is almost identical right? The only different is the block I passed into the map function.

The differences are highlighted in blue.

Extract the similar codes out and put them in the new method. I’ll call it w_avg then parameterized the differences.

Kept the grey part and parameterized the blue part.

Notes: Ruby can dynamically calls a function by passing string or symbol to the method send

I’ve remove the duplicate method now and our program look like this.

def weight_avg_gross_profit(reports)
  weight_avg(reports, 'gross_profit')
end

def weight_avg_cogs(reports)
  weight_avg(reports, 'cogs')
end

def weight_avg(reports, value)
  total_weight =   reports.reduce(0) { |sum, r| sum + r.sale_revenue }
  sum_prod = reports.reduce(0) do |sum, res|
    sum + (res.send(value) * res.sale_revenue)
  end

  sum_prod / total_weight
end

Now the logic of these two methods are in the same place. I can easily extend it to compute a weight_avg of any value from the report with a one-lined function.

Download it and run it

ruby parameterized_working_example.rb

Going further

Parameterization is a nice little trick but it is not a silver bullet. Sometimes it is wiser to have small amount of duplications than one over abstracted method than cannot be extended or modify. As Sandi Metz stated in RailsConference 2014


Duplication is far cheaper than the wrong abstraction.



Final Note

I believe we should focus not only on making things work but also on creating a clean code base that can be easily maintained by any developer carry on the project.
Like 265 likes
Tino
Share:

Join the conversation

This will be shown public
All comments are moderated

Get our stories delivered

From us to your inbox weekly.