Good Naming

Most developers struggle to name things well, and worse, many underestimate the importance of good naming. A badly named variable, method or class not only has the potential to infuriate other developers (we are an easily infuriated bunch when it comes to working with other people’s code), but it can damage your reputation. First impressions are¬†hard to change.

Naming things well is a skill which is both hard to learn and hard to teach. Nevertheless, any skill can be improved upon. If you want to improve your naming skills, my advice would be to improve your understanding of the English language. After all, you (hopefully) name things using words.

Firstly, make sure you know the difference between a noun, an adjective and a verb (and in particular an imperative verb). If you don’t, look this up.

In short, classes and variables should be named with nouns and methods (or functions) should be named using imperative verbs. The exception to these rules are booleans, including both boolean variables and methods returning a boolean. These often suit a name which resembles the question being answered and contains an adjective.

Below are a few examples – please note that I’ve used Pascal casing throughout for simplicity.

Here are some nouns which would make good class or variable names:

ProductController
UserFactory
Stack
ValidationHelper

And here are some nice imperative verb-based method names:

SaveProduct
DeleteUser
CreateDatabase
Pop
Peek
Clear
Connect

Finally, the following question-like names containing adjectives would be fine for boolean variables or methods (although my preference is to use the prefix “Is” whenever possible):

IsValid
Deleted
Online
IsInStock
HasPrimaryKey

These conventions come naturally to many, but unfortunately not all. So take care not to add to the frustration of the next person who will work on your code*.

*This may be you.

Share Button
  • MotorTorque

    Nice of you to address this topic and add some clarity, Ronnie. It’s surprising how tricky it can be for some people to name things correctly without this sort of convention (verb/noun/adjective etc.) to look at. An old quote I’ve seen flying around the internet “There are only two hard things in Computer Science: cache invalidation and naming things.” – Phil Karlton