My Naming Conventions

◀ Introduction▶ Why Name It?
Amazon By convention constant variables are all uppercase letters and local variables are a mix of lowercase letters, uppercase letters, and underscores. Personally, I like this convention because it creates distinctions among different contexts of variables.

On the other hand, how should we name a variable that means a combination of several things? For example, we may want to name a variable that keeps track of the number of ballots. We can name it numberofballots. To shorten it, we name it numofbal. Well, this seems fine, but to me a better name is numOfBal because we can make it as a rule that each uppercase letter means the start of a new logical word, known as camel case. Under this convention we can easily decrypt a variable’s meaning. I also use this convention for naming functions.


Some might argue that the name can also be NumOfBal, but a preferred convention is that names of objects start with a lowercase letter, and those of a class start with an uppercase letter.
Some people like to use underscores as separators, but they can quickly lengthen a name. On the other hand, names of constants should use all uppercase letters. Therefore underscores can be used in this case. For example, to name a constant integer that keeps track of ballots, one can do NUM_OF_BAL, or simply NUM_BAL, or BALLOT_COUNT.


In fact, if the program you are writing is a small one and you need to keep track of only one number, then calling it NUM may be enough.

As for source file name many use underscore or hyphen to separate words from each other. For example they may use android-syscall.h or android_syscall.h to name a header file that includes system call functions for the Android framework.

Personally I find hyphen to be a better choice because it's easier to type. You can adopt camel case in this case too if you so choose.

I have seen programmers use the prefix letter of a variable to indicate its data type. For example, sName refers to a string. This convention comes in handy when the same information needs to be represented in several different ways. For example, sName is a customized String variable and caName is a native character array.

On the other hand, many use the prefix letter to indicate a variable’s context. For example, prefix m or underscore indicates that it’s an instance variable of a class or a struct; c means that it’s an instance to a class; g means it’s a global variable. You get the idea.


The funny thing is that the naming convention you use highly depends on the code you are developing. Suppose you are maintaining your predecessors’ code base you should try to adopt their naming conventions.

Suppose the project you are working on uses some other open source code as the base (the popular Android framework for example) then you should try to adopt their naming conventions. The point is the less different your naming convention is from that of the environment the less confusion!

Since I’ve worked with the Android framework I have adopted their naming conventions as follows:

Element TypeNaming ConventionExample
Enum
Enum member
Struct
Struct member
Function
Variable
Underscore delimited
enum font_style {normal_style, goofy_style, formal_style};

struct color_value {
  int red_value;
  int green_value;
int blue_value;
};

int compute_sum (int first_int, int second_int) {
// return the sum of the given integers return first_int + second_int; }
Const variable
#define directive
Uppercase and hyphen delimited
const int BACKGROUND_RGB_VALUE;
#if defined(USE_WINDOWS)
#define MS_REC 16384
Source fileHyphen delimitedandroid-system.h
In any case, choose your own convention and stick to it for the rest of your programming life. I highly recommend that you pick a minimal set of conventions to minimize confusion. If you pick a different convention for naming each program element you may find it hard to remember.


A programmer following his rules long enough usually can remember the names of all variables, classes, objects, and other program elements, and he does not need to go back and forth to remind himself of the name of a certain variable, and therefore they increase efficiency and decreasing confusion.

On the other hand, let’s say I need 4 int to keep track of the ISBNs of four books, I would give them these names: isbn, isbn2, isbn3, isbn4.

The reason that the first variable does not have a number appended to it is that I usually do not know how many such variables I need. I name it without appending ‘1’ to it and if later I need more variables of the same nature, I begin appending numbers for distinction. An alternative is to use an int array obviously.

◀ Introduction▶ Why Name It?

fShare
Questions? Let me know!