GIDNetwork > Naming Conventions
Register
« Things to Avoid in C/C++ -- void main(), Part 10 Beginning Python Tutorial (Part 8) »

Naming Conventions

by: WaltP - Oct 24, 2005

NAMING CONVENTIONS

Definitely a matter of choice, I will outline various ideas for naming variables, functions, and the like for ease of readability. Let's tackle the obvious one first:

Variables

First and foremost, use names that make sense. If a variable is far-reaching (over many lines of code, or scope) it's name should reflect it's contents and/or use. For a variable with limited scope, a short form of it's use is generally acceptable. For example:

C/CPP/C++ Code Example:

by = fread(inbuf, sizeof(inbuf), numrecs, instream);

In this cast, by, the number of bytes read, is used simply to check if an error has occurred.

Some special variable used by convention are i, j, ... n, and variations such as ii, i1, i2, etc. In general, these variables are used as loop counters and temporary integers.

x, tmp, buf (among others) are also temporary values of whatever type is necessary. buf of course is generally an array or buffer.

p and q many times are used a temorary pointers, use in code that walks a buffer, p generally the source, q the destination. See K&R's code for string copy as an example.

Many conventions have been offered, but almost all agree on one thing -- the first letter is in lower case. They diverge from this point in most cases.

  1. All Lower Case -- All variable are defined in lower case alpha characters

    C/CPP/C++ Code Example:

    char *city, *state; 
    char firstname[20], lastname[25], gender; 
    int age, yearborn; 
    string homephone, workphone;
    
  2. Mixed Case -- Start with a lower case character, capitalize each consecutive word

    C/CPP/C++ Code Example:

    char *city, *state;
    char  firstName[20], lastName[25], gender;
    int    age, yearBorn;
    string homePhone, workPhone;
    
  3. Mixed Case, type designated #1 -- Start with a lower case character that designates the variable's type, capitalize each consecutive word

    C/CPP/C++ Code Example:

    char  *pCity, *pState;
    char   cFirstName[20], cLastName[25], cGender;
    int    iAge, iYearBorn;
    string sHomePhone, sWorkPhone;
    

    Using the character p before pointers is more universal than the rest of this style since pointers tend to be a special subtype anyway. g can also be added to designate a global variable.

  4. Mixed Case, type designated #2 -- Start with a lower case characters that designates the variable's type, capitalize each consecutive word

    C/CPP/C++ Code Example:

    char  *ptrCity, *ptrState;
    char   chrFirstName[20], chrLastName[25], chrGender;
    int    intAge, intYearBorn;
    string strHomePhone, strWorkPhone;
    

    The designator str is also used in place of chr for char arrays

    C/CPP/C++ Code Example:

    char   strFirstName[20], strLastName[25], chrGender;
    

    Other options include using the underscore (_) between words in a variable.

Functions

Generally upper and lower case and start with a capital letter.

C/CPP/C++ Code Example:

int  BlankEndOfLine (int col, int row);

void GetPosition    (int byt, int *col, int *row);
void DisplayNoRecord(void);

int  DisplayRecord  (int recNum, int recLen);
void DisplayHelp    (void);
int  LoadRecord     (int currRecors);

void UnLoadRecord   (void);

I haven't seen much else in this area as a suggested standard.

Macros and Defines

Again, just as simple as functions -- all upper case.

C/CPP/C++ Code Example:

#define SIZE     16 
#define START   (SIZE-1) * 5
#define SIZENEXT  2
#define ZERO     '0'
#define NINE     '9'

#define DOT      '.'
#define OPAREN   '('
#define CPAREN   ')'
#define EQUAL    '='

#define INDFILE  '@'
#define QUESTN   '?'
#define COMMENT  ';'

Problems / Limitations

There are so many conventions defined for namimg variables, functions, et al, you can get lost in all the advice. So as a rule of thumb, use one of these two conventions:

  1. the one your boss wants you to use

  2. the one that makes the most sense for your programming style

Would you like to comment? This story has been viewed 26,173 times.
« Things to Avoid in C/C++ -- void main(), Part 10 Beginning Python Tutorial (Part 8) »

__top__

Copyright © GIDNetwork™ 2001 - 2017

Another website by J de Silva

Page generated in : 0.02399 sec.