It happens in Rails’ generate scripts and it happens in a lot of plugins, but please, please, stop with the f blocks.
Too many times do I see:
form_for @bar do |f|
And even in the hpricot docs I see:
Hpricot(open(@url)) { |f| Hpricot f, :fixup_tags => true }
Why do I hate it so much?
The beauty of Ruby and the reason a lot of you use it is because it is pretty, easy to read and easy to write. It’s not Java. It’s not Ruby semantic, it’s just unreadable. F may stand for something, but to understand what it stands to, I have to read the earlier code.




It’s just an extension of Kent Beck’s pattern for naming the temporary variable in a Smalltalk
do:block - always call iteach, if the block gets complicated enough that you want to change the name, it’s time to extract a method and just call that in the block.In
form_for’s case,fdoesn’t seem like an awful choice, though I suppose there’s a case forform, otherwise the same rule applies - if the body of the form gets complicated enough that you want a more meaningful name for it, use a partial and passfin as its:object.In the Hpricot example, I’d be more inclined to use
it, or possiblyeach, but mileage varies.The rule of thumb is to standardize on particular parameter names in blocks of different points and, when you start to feel the pain of not having a descriptive parameter name, extract a method and give that a meaningful parameter name (or, better, make it a method of
f’s class if you can).I’m with Piers - this isn’t too harmful, as long as you’re consistent. Regarding passing f to a form partial, though, I have seen (and, I admit it, written) this monstrosity:
<%= render :partial => ‘form’, :locals => {:f => f} %>
I weep for the state of the world, with people (like me) who write this in it.
I agree somewhat.
F is not an awful abbreviation but I seem to find that once people start abbreviating often, their code gets sloppy and they abbreviate too often. Also, you only know what
fwhen you know the code, which seems anti-Ruby to me. Ruby was always meant that you could just pick up a document and read it to know what it does. I personally usemakeinstead offfor forms. It makes more sense in my mind.Ben has the perfect example of what I mean, and to me it feels like the Rails Core code is littered with those examples.
Sorry, guys, but using f as the form variable in Rails is just as standard as using i and j as loop variables in C (etc.). If it’s confusing, then the code it’s in is doing too much.
As for the overuse of abbreviations in general, that’s another blog post. But I never avoid a good practice because it can (theoretically) lead to a bad practice.
///ark
makehas a certain logic to it, along the same lines aseach.@Ben: Hang your head in shame! Actually, that code makes a good argument for using
formthroughout, that way, when you extract a chunk of code into a partial (which you will, inevitably, callform, you can magically pass it an appropriately named local variable by doingrender :partial => 'form', :object => 'form'. Obviously, any subsequent partials will need a:localshash, but I’m not sure that happens as often.@Zach: Use of abbreviations doesn’t have to be a slippery slope: the folks who slide down it would probably slide down some other slope anyway.
The key question anyone should ask themselves of their code is “Does this express my intention clearly”, sometimes a judiciously chosen abbreviation helps, at other times it hinders.
Piers. I appreciate the argument from BOTH sides of the fence.
I appreciate your last paragraph the most, I think that sums up my opinion the most and I whole-heartedly agree. That is what I have an issue with, it doesn’t express anything as a simple letter doesn’t necessarily mean anything, unless you look at the context. On one argument, it’s standard and most people know that f represents a form.
Saying that, I am not opposed to something like i for index. Maybe because I’ve ALWAYS known it to be that, and it’s not just Ruby that does it.
I found your blog via Google while searching for effexor xanax and your post regarding th The Dreaded f blocks. at ZachInglis.com looks very interesting to me.I just just wanted to drop you a note telling you how impressed I was with the information you have posted here. I also have websites & blogs so I know what I am talking about when I say your site is top-notch! Keep up the great work, you are providing a great resource on the Internet here!