What’s in a Name?

Notes on naming:

  • It’s easy to cause confusion between objects and name strings. In the statement below, are the objects strings? We don’t know.

    previousUser = user;

    For a user object, I use an unadorned word user:

    User user = new User();

    For a user name, I adorn:

    String userName = "George Washington";

    That way it’s clear throughout the code when I’m talking about an object, and when a name.

  • It’s also easy to make confusion about what exactly are the keys and values of a dictionary (hash). In hash users, are the keys user names? User IDs? We don’t know.

    I embed the word By in my hash name, to make it obvious what’s indexed by what:

    Dictionary usersByUserName = new Dictionary();

    All is perfectly clear here in the declaration, of course, but elsewhere in the code, it won’t be. Give your code maintainer (usually yourself, six months later) a break!

The key principle in creating a name is that the code will be read far more often than it will be edited. Make it easy to understand your code.

Hey, what are your pet peeves and great ideas for naming?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s