Named versus positional parameters

One place I think Rails dropped the ball.

I can’t stand positional parameters, and I don’t understand why we don’t use named parameters everywhere. For instance:

<%= link_to ‘log out’, {:controller => ‘/user/tools’, :action => ’logout’}, {:post => true, :confirm => ‘Are you sure you want to log out?’} %>

Just seems silly to me. You pass in the link text as the first parameter, the URL parameters as a subsequent hash and then the “HTML options” as a hash following that one. Confusing the issue more, you can leave out the {} when you’re only passing in one hash of options.

The problem I have with this is I have to expend the mental energy to remember what order I need to pass things in, and it’s just easier to remember concise, descriptive names.

Example:

<%= link_to :link_text => ‘log out’, :url_options => {:controller => ‘/user/tools’, :action => ’logout’}, :html => {:post => true, :confirm => ‘Are you sure you want to log out?’} %>

It’s slightly more typing, but at the advantage of creating self-documenting methods that are just easier to work with.

Recommend me on Working With Rails

Add to Technorati Favorites

Add to Google