Working Through the Railscasts - #2 - Dynamic find_by Methods
Continuing with the Railscasts, I found a good lesson through the example of Railscast #2, Dynamic find_by Methods.
The specific technical content was nothing new to me - the idea that there are many variations on the find_by method, and that there are some fun and effective ways to use them. If, however, you haven't gotten your head around this feature of Rails, do so now!
What happened, though, was that I decided to look at how we are using various find_by methods in our code. I came upon a controller method that included exactly two statements, like this:
1: a = A.find(params[:id])
2: b = B.find params[:id]
Of course, I already knew that in most cases, parentheses around method arguments are optional. As an older programmer (wheeze, cough), I like parentheses - they neatly encapsulate the arguments and make for clear reading, to me. However, I accept that there's a different kind of readability that comes with a language that doesn't require them.
Rails had, before 2.0, been seemingly somewhat inconsistent about when it did and did not require parentheses. At least, it seemed inconsistent to me.
Regardless of that, my problem was in seeing two lines of code, one directly below the other, that did the same thing on two different models with two different coding approaches.
Given that not only our development team, but our customers, will be reading this code, a certain consistency seems to me to be of value.
So while I didn't learn anything new about find_by methods from this Railscast, I did have an opportunity to add something to our discussion of our programming style.