next up previous
Next: Structure/Modularity Up: C++ Programming Style Previous: Principles

Names

In order to tell a story, you have to have names: Jack and Jill, the bartender and the patron, or whatever. If you're going to describe how a program works, you have to name the parts, both the data and the functions. Names are a critical foundation upon which the story of your program is built.

Rule 2   All names should be descriptive and accurate.

Rule 3   Be consistent with capitalization and punctuation of names. Decide on a scheme and stick with it.

Because C and C++ are case-sensitive, you can distinguish different kinds of things with capitalization, but be careful about this, because humans are not always case-sensitive. How many times have you had to ask whether it's ``String.h'' or ``string.h''? People, particularly computer-people, can train themselves to be case-sensitive, but it's always easy to overlook.

Here are some rules that are in common use or that are reasonable.

Creating new types--classes and structures--is more rare and important than naming a variable or function. Therefore, it's reasonable to signal this by a capital letter. Constants are usually even more rare, so the uppercase names will be few. At least, this is a sensible view, even if there are exceptions.

You can use other schemes if you prefer. Your book, for example, typically uses uppercase for new types, so they would write LIST or NODE. This is acceptable as well. However, do not forget to be consistent. Don't name one variable CURR and the next prev because they will look like completely different kinds of things, and the reader will be confused and then annoyed.

Note that a good name often has two or three words in it. The issue always arises how to separate them. For example, a variable representing the smallest list element should not be named smallestlistelement. Instead, it could be named smallest_list_element, separating the word by underscores, or smallestListElement, indicating word separation by capital letters. Both ways are commonly used, so use whichever you prefer. I think underscores are easier to read, but either is acceptable. As always, be consistent.


next up previous
Next: Structure/Modularity Up: C++ Programming Style Previous: Principles
James Hale
2001-09-19