When readability is better than metaprogramming
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.


Recent comments
1 year 23 weeks ago
1 year 23 weeks ago
1 year 25 weeks ago
1 year 27 weeks ago
1 year 42 weeks ago
1 year 45 weeks ago
1 year 45 weeks ago
1 year 45 weeks ago
1 year 46 weeks ago
1 year 48 weeks ago