C Coding Style Guidelines
CPE 101
Instructor:
A coding style is nothing more that a set of rules for formatting code to make it readable by people. Using a consistent coding style makes it easier for you to read your own code making errors easier to find and fix. Another benefit is that it makes your code more readable for others. This quarter that will primarily be a benefit to your instructor and the graders that evaluate you code. In the future it will benefit your coworkers and consumers of your programs. You will find that programmers all have there favorite styles - over time you will realize that using a style, any style, is more important than which particular style. For this class I am going to ask you to follow a few style rules - realize that a complete set of style guidelines would be considerably more complete that this one.
The following header MUST be included at the top of each of your source files:
/* Your name
CPE-101-section number
*/
Choose variable and type names carefully. X, jj, and b2 don't mean as much as timeLeft, biggest, and whereNext. Variable names should be meaningful, and generally not single-letter.
If you decide to abbreviate a commonly used term in variable or type names, use exactly the same abbreviation everywhere. (For instance, if the term "category" appeared often in variable or type names, you might abbreviate it everywhere to "ctg".)
In multiple-word names, capitalize the first letter of the second and later words. Do not use underscores. Leave the first letter lowercase.
Examples: stringLength, nameNdx, bestScore, isNegative, calculateAverage
Do not use "magic numbers". There must be no actual numbers other than 0, 1, -1 or 2 in your code. Even these might sometimes be constants if they could change in later versions of the code.
Use all caps for constants, and underscores to separate words in this case.
Examples:
#define SPEED_OF_LIGHT 299792458
#define PI 3.14159
if (i == 5)
{
/* Do Something...
*/
i += 7;
}
Never this! (even though it is syntactically correct)
if (i == 0) {
/* Do Something...
*/
}
if (i == 0)
{
j++;
}
Never this! (even though it is syntactically correct)
if (i == 0)
j++;
And never this! (even though it is syntactically correct)
if (i== 0) j++;
if (thing1 !=
thing2)
{
/* Do something useful
*/
}
else
{
/* Do something else useful
*/
}
if
(condition1)
{
/* Do something
*/
}
else if
(condition2)
{
/* Do something different
and useful */
}
else
{
/* Do something completely
different and useful */
}
for (i = 0; i < x; i++)
{
for (j = 0; j < z;
j++)
{
/* Do something useful!
*/
}
if (isHardToMakeMeaningfulExamples)
{
/* Do something else useful
when something is true */
}
whatever++;
}
while (*n < MAX_RUNS &&
fscanf(runFile, "%s %lf %d %d", list[*n].name, &list[*n].distance,
&list[*n].prSec,
&list[*n].count) != EOF)
Horizontal whitespace:
Use whitespace to clarify your code and to break up long expressions, but don't overdo it.
Put spaces after each comma, and around each keyword (note that "if", "while" and "for" are keywords). Put a space after each semicolon in a for header. Never put a space before a comma or semicolon.
Put spaces around operators, except for operators on the top two rows of the precedence table (e.g. [], ++, &) or in very large expressions where you may avoid spaces around the innermost operators. Don’t have more than three variables or operators in a row without a blank space.
Good: epsilon = 2*beta[1] + *gamma - delta*pi;
Bad: epsilon=2**gamma-delta*pi;
Put single blank lines after each function, and between local declarations and the function code.
Don't put space after an opening paren, or before a closing one. Do not put space between a function name and the opening paren for the parameter list.
Style Examples
Example of Good Style:
int test(int number)
{
int count;
for (count = 1; count < MAX; count++)
{
if (number+1 > i && number-1 < count)
{
printf("hello\n");
}
else
{
printf("goodbye\n");
}
number = number + 1;
}
count = number;
while (count > MAX)
{
count = count - DECREMENT;
}
}
Example of Bad Style:
int Test( int n ) { <-- uppercase name, { on same line, spaces before/after parentheses
int i; <-- one letter name, no bland line after variables
for (i=1;i<5;n=n+1,i++) <-- bad horizontal space, use of constant
{
if (n+1>i && n-1 < i) <-- Single letter variable names, poor spacing
{ <-- double indentation
printf("hello\n");
}
else { <-- brace on the same line
printf("goodbye\n");
}
}
i = n;
while(i > 10) <-- no space after "while", use of constant
i = i - 2; <-- no braces (compound statement)
}