C++ Language Coding Style
CS Home Page: Web Course Materials: C++ Coding Style
Delimiter Pairs
Braces { } and the comment delimiters /* */ must be lined up so that they are either on the same line or in the same column.
Example:
Incorrect example:
...
}
You must indent your code inside braces.
Three spaces are required per indentation level. (This can be set by selecting Properties from the View menu.) The body of a for loop or while loop or the statement following an if or an else must be indented one level (3 spaces).
Example:
for (j = 0; j < i; j++)
{ a[i][j] = 1.0 / (i + j + 1);
a[j][i] = 0.0;
}
for (j = 0; j < i; j++)
{ a[i][j] = 1.0 / (i + j + 1);
a[j][i] = 0.0;
}
Use blank lines to separate logically separate parts of a function.
Example (notice the blank line between the two for loops):
/* --do n delete_max operations
*/
for (i = n; i >= 2; i--)
{
swap(&a[1],
&a[i]);
perc_down(a,
1, i-1);
}
Leave no blank before and one blank after a comma or semicolon.
Example:
blanks near semicolons
*/
search_down(i, n); /* blanks near commas */
Leave one blank before and one after each binary arithmetic, logical, and assignment operator.
Examples:
if ((a == b) || (a < c))
Separate each function from the next with a single line of the following format:
Comments are designed for the human reader. That person may be you six months from the time you first wrote a program when your boss asks you to add some feature to your program or it may be someone who will have to take over your program when you advance to another job. Write comments so that someone unfamiliar with your program can understand what you did.
Some of the best comments are good names for variables. If you name a variable cost_per_item instead of c, it will help the human reader to understand what you are doing.
Some general rules:
Comment as you code. If you comment after your program has been written, the comments will never be as clear and precise. Moreover, good comments will help you crystalize your thinking and help you to get a program debugged sooner.
When you declare a new variable, comment its use. Never again will you know so well why you needed it.
When you first are thinking of writing a function, write the function statement and the function header comment before you begin to write the function. If you don't understand precisely what a function is supposed to do then you need to think about it some more BEFORE writing it.
Do NOT comment the obvious:
The following rules are to be used to decide when to use uppercase and lowercase letters in identifier names.
All variable and function names and all class members are in lowercase with perhaps an occasional uppercase letter as in a name like oldString or an underscore character as in a name like new_value.
All constants are in uppercase with perhaps anoccasional underscore as in MAX_SIZE.
All typedef and class names start with an uppercase letter and are followed by lowercase letters (again with the possible use of an uppercase letter or underscore as in PartsRecord).
Use names that tell you what the variable or function does. For example use string_length instead of sl or next_iterate instead of something like xn. Use previous_x, current_x, and next_x instead of something like x1, x2, and x3.
Putting Together a Source Code File.
A C++ source code file contains the following items must appear in the following order:
The constant definition section contains all constants used in the program. For example,
The class definition section contains all class definitions (if any are present):
class Class_name
{
public:
constructors
mutators
accessors
private:
data
};
The global variable definition section contains all global variables. All global variables must be commented and initialized. You can have AT MOST THREE global variables per module. You should be very reluctant to use global variables. Because they are at risk of being modified by ANY function in your program, they can be a serious source of bugs.
The function definition section contains all functions. Some rules about functions:
Functions can be at most 30 lines long. Comments, blank lines, and lines containing only braces are not included in this count. Functions that consist of one long if/else statement may be longer, provided that each branch is ten lines or less.
Sort functions in such a way that each function is defined before the location in which it is first used. This means that main should be the last function in your program.
If Statements.
In if statements, the branches of the if statment can follow on the same line as the if or else if they are one statement or one statement and a return. Otherwise, put them on a new line.
Example;
Example:
Avoid the problem by always using { } as follows:
Example (if you want the else to belong to the first if):
Only use for loops when a variable runs from somewhere to somewhere with some constant increment/decrement.
Example:
Do not use the break, continue and goto statements. Use a bool variable to control the execution flow.
Magic Numbers.
Use const definitions to define constants in your program. Don't forget that all such constant names must be all UPPERCASE.
Example:
All buffers (vectors that are used to hold data temporarily while it is processed) must have their lengths defined as constants.
Example: