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 = 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?