By Mislav on 12 Feb 2009 in rails
Ruby developers are very sensitive to naming things. There are strong conventions regarding class, variable, method and file names, and these are not just organizational or cosmetic – some have their semantics even during code execution. For instance: in Rails a model named “Person” is implicitly tied to the “people” table in the database just by name inspection.
In our recent project we’ve been building lots of our application logic around naming. Today I was editing some code and have been blown away realizing how deep we actually went with this over time.
Take a look at this single line of code from one of our main Haml templates:
= thumbnails_box "Top contributors", :expires_in => 30.minutes, :link => true
This renders a generic “thumbnails box” view partial populated with avatars and names of several of our top contributors. What isn’t obvious here is just how much this single line knows:
top_contributors_path named route (triggered by :link => true);@top_contributors instance variable;@top_contributors variable is created in the controller by paginating the top_contributors named scope of our models;top_contributors_page parameter to differentiate between other paginated collections on this page;If you read the above carefully, you have noticed that a single string “Top contributors” – that started off as a HTML title – went full-stack: from models to controllers, view templates, caching logic and even JavaScript. All by the power of naming.
One line of Ruby code = one idea. Still, this proves there’s no limit on how sophisticated that idea might be.