Refactoring and DRY
Thursday, May 01, 2008
I have been at work refactoring code and striving to DRY (Don’t Repeat Yourself). I took duplicate code out of the home page and the all posts page and turned it into a partial to view the Post teasers. For some reason I have in the past fallen in love with the each method, so tended to write:
Array.each do x
x.foo
end
where I could more concisely use:
for x in Array
x.foo
end
I guess it’s because I’m used to appending methods like sort or reverse on the end of a collection, and fell into adding each to the end when I needed to iterate through the collection. Also, I tended to use each because I was often passing a block (using the braces notation) to it.
With all this in mind, I had a bit of clean up to do in my views.
Until next time,
Tom Porter