Overabbreviation

Bad examples:

passwd

fname

cust

o

Good examples:

password

first_name

customer

order

Explanation: Abbreviations like passwd violate the Principle of Least Astonishment. If I know a class has a password attribute I should be able to guess that that attribute is called `password`. Since `passwd` is pretty much totally arbitrary, it’s a practically unguessable. Plus, how valuable is it to save two keystrokes?

I have a saying: It’s better to save time thinking than to save time typing.

Exception: Abbreviations are fine when they match up with real-world abbreviations. For example, SSN , USA and admin are all fine because these are predictable and already familiar to most people.

Naming Scalars Like Objects

Bad example:

store = "Jason's Capsaicin Station"

Good example:

store_name = "Jason's Capsaicin Station"

Explanation: If I see a variable name like store , I think it’s an object. This problem becomes especially severe when the codebase does in fact have a class called Store and some store s are instances of that class and some store s are just store names.

Naming Counts Like Collections

Bad example:

products = 0

Good example:

product_count = 0

Explanation: If I see a variable called products I would probably expect its value to be a collection of some sort. I would be surprised when I tried to iterate over the collection only to discover that it’s actually an integer.

Naming a Variable After Its Type

Bad example:

array = []

Good example:

customers = []

Explanation: A variable called array or hash could contain pretty much anything. By choosing a name like this you’re forcing the reader to do all the thinking.

Use of Mass Nouns For Collections

Bad examples:

news[0]

inventory[0]

Good examples:

articles[0]

inventory_items[0]

Explanation: There are certain words that have no singular form. Examples include “stuff”, “garbage” and “laundry”. This anti-pattern especially applies to database table names.

Key/Value Mystery

Bad examples:

products.each { |key, value| puts value }

products.each { |k, v| puts v }

Good example:

products.each { |name, price| puts price }

Explanation: Names like key and value have almost no meaning. There’s almost always a more purpose-revealing option.