Initiation by fire – part 2


Beginner mistake number 2:

Names and idioms. One thing I learned about the Rails community is that all the good developers secretly wish that they were English majors and Math majors at the same time. I know it sounds silly, but Ruby devs will call you down for using a crazy model name while at the same time praise you for using a complex idiom that nobody can read unless they understand the idiom. One of my first forays into RoR development was a site called LongTimeLost ( This is also the first site where I started working with other Rails developers and learning what is acceptable and what isn’t when building a Rails site. After a developer took one look at my code he immediately noted that ‘looking4people’ is a horrible name for a table, and at the time I couldn’t understand what he was talking about, no one but us was going to see the code, what did it matter what I named things (I admit now looking back on my choice for a table name I’m quite embarrassed). It turns out in Rails naming is important. Rails does so much magical box stuff based on what you name things, have a table called users in your database? Well the way to search it with active record is User.find. The naming also affects how easy it is to read parts of your code. Looking4Person.find really isn’t good reading and Rails above most else should be readable.

The dichotomy of Rails though, is idioms. Idioms, which can cut multiple lines of code into one line. For example, probably the most popular idiom takes this code:

if foo=’bar’
puts ‘a’
puts ‘b’

And turns it into:

puts (foo==’bar’ ? ‘a’ : ‘b’ )

Clean, elegant, and if you don’t know the idiom, you have no idea what it’s doing. Thus is the way of idioms. The best place to keep up to date on idiomatic code is There is no hard and fast way to learn them, mostly they come by seeing them, saying “Wow, that is cool, what does it do exactly?” and then remembering it so you can impress others later.

Naming on the other hand does have a hard and fast way to do it. For the most part imagine what you are going to name your objects in sentences and ask your self if they make sense the best ways are the associations (which I’ll write about later) and the find method.
Looking4Person.find (a good example of a bad name)
User.find (a good name)
Now with the associations:
A User has_many Relationships
A Group belongs_to Admin
A User has_many Friends through Relationships (the has_many :through will be talked about later)

Variable names also should also avoid camel case like the plague. Camel case, while the standard for languages like Java is something that will make an experienced RoR developer’s blood boil. And I’ll give you an example why, which is easier to read:
a) authenticationMethodGoesHere
b) authentication_method_goes_here
The answer is obviously b, it looks more like English and is far easier to read at a glance, which is what Rails developers love.

2 Comments (+add yours?)

  1. will
    Oct 15, 2007 @ 22:21:44

    Some interesting stuff about naming! Interestingly, naming is a subject that comes up quite a lot in the world of databases as well. I thought I’d add some considerations from the database point of view for everyone to think about.

    1. There are a ton of great books on design, naming, etc. My personal favorite is Joe Celko’s SQL Programming Style.

    2. The character ‘_’ is a reserved character in LIKE comparisons in SQL…it stands for any single character.

    3. If you have time (like, a week?) check out the ISO/IEC 11179 standards, which detail what is appropriate for data element names in general according to ISO.

    4. The best piece of advice that I ever got for naming was “name it what it is, if people call your data something in the real world, name it that…and take care to pay special attention to multiple vs. single sets like employee vs employees.”

    If you like reading out this stuff, I’d encourage reading some of Celko’s older posts on the Microsoft SQL newsgroup, he REALLY tears into people…tough love at its finest. (microsoft.public.sqlserver.programming)

Leave a Reply