L-exp Mobile

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.



Options:   Save This | Share
Viewed 0 times
Published 4 months ago
By david
From Resource Thinkincode.net : David Calavera Palacio in lists:
Conferencia Rails Hispana '07

Menu

by Genís