Pick Natural Names (while programming, especially testing)

I’m reading a wonderful book named “Agile Web Development With Rails“, which is about how to develop web applications quickly (agilely) with Ruby on Rails.

In the chapter on how to do testing with rails, it gives out an interesting suggestion: use natural names like “Christmas_order”,”Fred” instead stiff names like “order1″,”customer1” we are used to. I really like this idea!

Just like the names of variables in general, you want to keep the names of fixtures as self-explanatory as possible. This increases the readability of the tests when you’re asserting that product(:valid_order_for_fred) is indeed Fred’s valid order. It also makes it a lot easier to remember which fixture you’re supposed to test against without having to look up p1 or order4. The more fixtures you get, the more important it is to pick good fixture names. So, starting early keeps you happy later.
But what to do with fixtures that can’t easily get a self-explanatory name like valid_order_for_fred? Pick natural names that you have an easier time associating to a role. For example, instead of using order1, use christmas_order. Instead of customer1, use fred. Once you get into the habit of natural names, you’ll soon be weaving a nice little story about how fred is paying for his christmas_order with his invalid_credit_card first, then paying his valid_credit_card, and finally choosing to ship it all off to aunt_mary.
Association-based stories are key to remembering large worlds of fixtures with ease.

Join the Conversation


Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  1. Hi there !

    I just stumbled upon your blog and I have a question for you if you don’t mind…

    I also have a WordPress blog, mostly in English but also in French and I have a few posts in Chinese. I was on Blogger before and the characters showed up just fine. But on WordPress, they’re just a bunch of “????”.

    So I’d be curious to know how you got the chinese encoding working, and which software you use to write in Chinese.

    Sorry for the questions… It’s just that I spent 12 years of my life learning Mandarin, si may as well use it ! :lol: