CS2141 Style Guidelines

Indentation

There are many opinions on how to indent code. There is one rule that can be agreed upon. Be consistent. In order to enforce this consistency, each indentation must exactly 4 spaces.

Functions

The code for the function should begin 4 spaces from the left. For example:

int Max (int x, int y){
return (x < y) ? y : x;
}

IF-ELSE

The code for IF-ELSE should be indented as follows.

   if (AVariable < MaxAmount) {
AVariable = MaxAmount - 1;
return AVariable;
} else {
return MaxAmount;
}

FOR, WHILE, DO Loops

The indentation for loops is similar to the IF-ELSE statements. Examine the following examples.

   for (int i = 0; i < MAX; ++i) {
A[i] = i;
}

while (i < MAX) {
A[i] = i;
++i;
}

do {
A[i] = i;
++i;
} while (i < MAX);

Class definitions

The body of a class definition should be indented:

class Cow
{
public:

Cow( ); // Default constructor

void eatHay( int amount ); // This cow eats the given amount of hay
void moo( ); // This cow moos.

private:

int age; // Age of this cow
};