Coding Standards
Professional programming teams usually use some kind of coding standard,
to guarantee that everybody's code has a uniform look. In addition, some
rules can also be designed to protect less knowledgeable programmers from
making common mistakes. Lack of adherence to the coding standard rules
below will affect your grade.
Rules
-
Declare variables where you first use them, and not at the top of the
current block.
-
Regular function names should contain no underscores and start each
word with an uppercase letter, e.g. "DestroyTheWorld" and not
"Destroytheworld" or "destroy_the_world".
-
Class member function names should start with a lower case letter but
then use uppercase letters for the first letter of the second, third or
further words, e.g. "saveTheWorld".
-
Variable names should be all lowercase and individual words should be
separated with underscores, e.g. "error_count".
-
Use all uppercase for declarations of constants (except for function
argument lists) and enumerated values, e.g. "const int MAX_LENGTH".
-
Compile all files with the -Wall -W -Werror command line options.
-
Use the ANSI C++ standard header files, e.g. <iostream>,
<cstdio> and <cstring>, not <iostream.h>, <stdio.h>
or <string.h>.
-
Use highly descriptive names for functions and variables. This is
very important for you and others that have to read your code later. Please
remember, real-world programs are typically maintained and modified for
years!
-
Check return values of all non-void function calls for error returns.
If your code detects an error you either want to take a corrective action
or at the least issue a very specific error message and
abort the program [call "std::exit(some_error_code)"].
-
Use exactly two blank lines between each function.
-
Put single spaces around all operators.
|
Like this:
|
Not like this:
|
|
i += k + 7;
|
i+=k+7;
|
-
Put single spaces before the opening parentheses in "if", "while",
"switch" and "do" statements but never between a function name and its
argument list.
|
Like this:
|
Not like this:
|
if (i < 3)
{
j += 17;
DoStuff(k, 12);
}
|
if(i < 3)
{
j += 17;
DoStuff (k,12);
}
|
-
One of the most debated rules is that of where to put the opening
brace. The two most used conventions are:
|
At the end of the line starting the block:
|
On the next line, appropriately indented:
|
for (int i = 0; i < NUM_ROWS; i++) {
for (int j = 0; j < NUM_COLS; j++) {
sum[i][j] = i + j;
dif[i][j] = i - j;
}
}
|
for (int i = 0; i < NUM_ROWS; i++)
{
for (int j = 0; j < NUM_COLS; j++)
{
sum[i][j] = i + j;
dif[i][j] = i - j;
}
}
|
Personally, I prefer the second one (on the next line), but either
one will be accepted. No other convention will be accepted, however.
-
Do not use tabs to indicate indentation levels. Use 3 blank
spaces per indentation level, instead. Most editors allow you to have so
many spaces automatically inserted every time you press the tab key. Set
yours to automatically insert 3 spaces.
-
Keep each line of your code no longer than 75 characters. Long lines
must be broken and indented appropriately to allow easy reading.
Like this:
// this function updates the 3-dimensional position of
// a monster with a given id.
void updateMonsterPosition(int monster_id,
int monster_vel_x,
int monster_vel_y,
int monster_vel_z)
{
// ...
}
Not like this:
// this function updates the 3-dimensional position of monster with a given id.
void updateMonsterPosition(int monster_id, int monster_vel_x, int monster_vel_y, int monster_vel_z)
{
// ...
}
Adapted, with permission, from a similar document originally
written by Dr. Johannes
Ruscheinski.
© 2003 Wagner
Truppel. All rights reserved.