C Coding Standards Checklist
Sample Code
Layout
[ ] Indent source code in a manner consistent with the logical
structure of the code.
[ ] Indent comments the same amount as the code they describe.
[ ] Four spaces should be used as the unit of indentation. Never place
tab characters in source code files.
[ ] Avoid lines longer than 80 characters.
[ ] Open-close brace pairs must always be vertically aligned.
[ ] Each line contains at most one simple statement.
[ ] Braces are used around all statements, even single statements, when
they are part of a control structure, such as an if-else
or for
statement.
[ ] One blank line is used in the following circumstances:
- Between functions
- Between the local variables in a function and its first statement
- Before a block or single-line comment
- Between logical sections inside a function to improve readability
Comments
[ ] All comments should be clear, correctly spelled, grammatically
correct, and use simple English phrases.
[ ] Comments should be meaningful and relevant. They should assist
the reader in comprehending the code by adding
helpful annotations. Avoid comments that do not significantly enhance
readability and comprehension. The comment should not simply restate
the
code.
[ ] Comment any statement whose intent is not immediately obvious.
[ ] Every control structure has a descriptive comment on the preceding
line.
[ ] Each declaration must be on a separate line and must have an
explanatory
comment. Variables local to a function may use end-of-line
comments.
int level; /* indentation level */
int size; /* size of table */
[ ] In most cases, initialize local variables where they're
declared.
The only reason not to initialize a variable where it's declared is if
the initial value depends on some computation occurring first.
[ ] Put declarations only at the beginning of blocks. Don't wait to
declare variables until their first use. The one exception to the rule
is indexes of for loops, which in C can be declared in the
for
statement.
[ ] The header block should contain the program name, student name, and
course-section number.
Naming Conventions
[ ] Functions Function names should be verbs or verb phrases,
in
mixed case with the first letter lowercase, with the first letter of
each
internal word capitalized. Examples: run();
isUpperCase();
getBackground(); findTotalCost(int Years);
[ ] Variables Variable names are nouns that succinctly
convey as
much
information as possible about what the object is.
[ ] Choose names that are "psychologically distant."
E.g. int total1, total2; would be better as int
boyTotal,
girlTotal;
[ ] One-character variable names are not allowed.
Coding Style
[ ] Don't assign several variables to the same value in a single
statement.
[ ] Use for only for counting loops, not conditional loops.
[ ] Never use break or continue in a loop.
[ ] Never compare a boolean value (or boolean function call) to a
boolean
constant.
[ ] Make the structure of your program match the intent.
NEVER DO THIS:
if (booleanExpression)
{
return 1;
}
else
{
return 0;
}
It is much clearer and simpler to write:
return booleanExpression;
Design guidelines
[ ] There should be no redundant code.
[ ] Decompose the problem into appropriate subprograms.
[ ] Subprograms must be cohesive. (Pg 175).
[ ] Design subprograms with appropriate parameters and return type.
Never
use global variables.
[ ] Use structured flow-of-control constructs.