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.

The technique that I often use and find it’s super easy to do is

Finding weight average

There are reports that I need to find a weight average of a

```
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

```
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

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.

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

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

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
266 likes

From us to your inbox weekly.