|
||||
|
« Things to Avoid in C/C++ -- void main(), Part 10 | Beginning Python Tutorial (Part 8) » |
Naming Conventions
by: WaltP - Oct 24, 2005
NAMING CONVENTIONSDefinitely 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: VariablesFirst 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.
FunctionsGenerally 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 DefinesAgain, 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 / LimitationsThere 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:
|
GIDNetwork Sites
Archives
Recent GIDBlog Posts
Recent GIDForums Posts
Contact Us
|
« Things to Avoid in C/C++ -- void main(), Part 10 | Beginning Python Tutorial (Part 8) » |