RailsConf Europe - Wednesday Keynotes

September 19th, 2007

Partly due to having to check out my hotel, partly due to my hangover and partly due to not being bothered I missed today’s pre-roller advert by ThoughtWorks to today’s keynotes.

But i’m here now for the session on Best Practices. A bit late. Siting at the back nursing my headache.

The Best Practices session is very codey but still entertaining so far covering test-first, associations, method naming and chaining. Expressive Interfaces they call them, readability and undestanding. Now moving on to with_scope and the dangers of using a before_filter with a scoped command, hence reducing all queries to a limited scope. For instance only finding posts for the current user. Instead use associations:


current_user.posts.find(params[id])

But it has uses. You can use method_messaging with a with_scope and create a dynamic has_many association through a proxy. Similarly very cool but I can see that being mis-used also. I think using method_missing should be a last resort, documented heavily and only used if it significantly improves readability or time. Not really as a best practice.

The use of conditions and commands in Ruby can be confusing as there is many ways of doing it. The question, which can be understood quickest?


command if conditional?
....
if conditional?
  command
end
...
conditional? and command
...
conditional? && command
...

All of the above do the same. For me number 2 is clearest but involves more typing, but I hate mixing up commands with conditionals in an expression, and the speakers agree.

Sorry, comments are closed for this article.