When readability is better than metaprogramming 1

Posted by david

This morning I've bumped into this code:


latitude = phif + (x2frac * x2poly * (x ** 2)) + 
  (x4frac * x4poly * (x ** 4)) + 
  (x6frac * x6poly * (x ** 6)) + 
  (x8frac * x8poly * (x ** 8))

longitude = central_meridian + (x1frac * x) + 
  (x3frac * x3poly * (x ** 3)) + 
  (x5frac * x5poly * (x ** 5)) + 
  (x7frac * x7poly * (x ** 7))


And quickly I've thought that it could be refactored easily with a loop like this:


latitude = phif
longitude = central_meridian + (x1frac * x)

(2..8).each do |n|
  latitude += eval("x#{n}frac * x{n}poly * (x ** #{n})") if n % 2 == 0
  longitude += eval("x#{n}frac * x{n}poly * (x ** #{n})") if n % 2 != 0
end

both pieces of code work fine, but sincerely, my mind is getting used to understand the first one better than the second one, so I've ruled out my refactor and I've kept the original version.

Comments

Leave a response

  1. KorepetycjeSeptember 13, 2008 @ 12:28 PM
    Did you check both of them using Profiler?
Comment