C++ Style Rules for 420-406-AB

 

Style refers to things such as

·         Indenting code

·         Picking meaningful name for variables

·         Writing one declaration per line

 

The purpose of using a consistent and good programming style is to make your program easy to read.

 

A program that is easy to read

·         is easy to make changes to, and

·         has fewer errors

 

 

SR-1

Do not submit listings with wrapped lines in them. To print your source code, read it into a word processor, adjust the margins and as required change the font size so no lines are wrapped. 

 

Use a fixed width font such as Courier. 

 

If lines of code are very long you might have to print in landscape.

 

SR-3

Begin the program with a very brief description of what the program does (not of how the program works.)

 

Writing a program description that is wrong or misleading is even worse than not writing a description at all.

 

SR-4

Write code that can be read and understood quickly without any extra documentation.  If necessary, add a few comments to explain what your code is doing.

 

SR-5

Don't code comments that state the obvious.  E.g.

 

x = 0;  // Assigns 0 to x

 

SR-6

Separate pieces of code with blank lines.  E.g.

 

Code blank lines before and after a group of #include statements.

Code blank lines before and after a group of constant declarations.

Code blank lines before and after a group of function prototypes.

Code blank lines before and after a group of variable declarations.

 

SR-7

Do not yell!! in messages.

 

SR-8

Don't use global variables.

 

SR-9

Variable identifiers begin with a lowercase letter.  E.g. count

 

SR-10

Run the words in multiword identifiers together.  The 2nd and subsequent words begin with a capital letter. E.g.       

 

parenthesesCounter

ageEmployee

countStudents

 


 

SR-11

Code a symbolic name for filenames, array dimensions, and mathematical constants.  E.g.

 

const char   FILENAME[]     = "a:data.txt";

const int    MAX_SIZE_NAME  = 40;

const double PI             = 3.142;

 

SR-12

Constant identifiers are coded in all uppercase.   E.g.

 

const int    MAX_INT       = 2147483647;

const double PI            = 3.142;

const int    MAX_SIZE_NAME = 30;

 

enum ColourType { RED, GREEN, BLUE};

 

(Exceptions: true, false)

 

SR-13

Code multiword identifiers for constants by separating the words with underlines.   E.g.            

 

PI_SQUARED

 

SR-14

Place symbolic name declarations at the top of the source file, above main()

 

SR-15

Type identifiers begin with an uppercase letter.  E.g.

 

typedef char[MAX_SIZE_NAME+1] NameType;

 

struct PointType

{

   double x;

   double y;

}

 

class StackType

{

   ...

}

 

(Exception: built-in types e.g. char, int)

 

SR-16

Code only one declaration per line.  E.g.

 

int i, j;

è

int i;

int j;

 

 

SR-17

Use one or other of the two following styles.  Do NOT mix the two styles.

 

Function identifiers begin with a lowercase letter.  E.g.       

 

max()

areaOfCircle()

 

Constructor identifiers begin with an uppercase letter.

Function and constructor identifiers begin with an uppercase letter.  E.g.    

 

Max()

AreaOfCircle()

 

(Exception: main(), standard library functions e.g. strlen())

 


 

SR-18

Code meaningful identifiers for variables, constants, types, and functions.   E.g.     

 

closingSymbol

PI

NodeType

Sort()

 

Do not use long vague identifiers such as 'subscript'

 

Choosing a misleading identifier name is even worse that coding a meaningless name.

 

When an identifier is not meaningful (e.g. i) add a comment that describes the identifier's purpose.  E.g.

int i;     //  index to client[]

 

SR-19

Code a space on each side of the following operators.

 

Bit shift operators "<<", ">>".  ("<<" and ">>" are also used for stream extraction and stream insertion respectively.)  E.g.

 

cin >> xPos;

 

Multiplication "*", division "/" , and modulus "%" operators.  E.g.

 

area = height * width;

 

Initialization operator "=".  E.g.

 

int xPos = 10;

 

Assignment operators  "=, +=, -=, *=, /=, %=, >>=, <<=, &= ^=, |="  E.g..

 

nClient = 45;

 

Binary logical operators   "&& ||"

 

Binary bit string operators "& |"

 

 

SR-20

Code a space between the keywords for control structures and the following opening bracket "(".  E.g.

 

while ( i < n )

    ...;

 

for ( i = 0; i < n; i++ )

    ...;

 

if ( i < n )

    ...;

 

switch ( ch ) {

case '(' :

    ...;

    ...;

    break;

case ')' :

    ...;

    ...;

    break;

}

 

SR-21

Code a space after the ";" separators in a for loop header.  E.g.

 

for ( i = 0; i < n; i++ )

    ...;

 

SR-22

Align a function's opening and closing braces.  E.g.

 

int Min( ... )

{

    ...

}

 

SR-23

Either use the classical style for positioning control structure's opening and closing braces " { } " or align the opening and closing braces.  Do NOT mix the two styles. E.g.

 

classical style

 

if ( ... ) {

    ...

    ...

}

else {

    ...

    ...

}

if ( ... )

{

    ...

    ...

}

else

{

    ...

    ...

}

while ( ... ) {

    ...

    ...

}

while ( ... )

{

    ...

    ...

}

switch ( ... ) {

case 1:

    ...

   break;

case 2:

    ...

    break;

default:

    ...

}

 

switch ( ... )

{

case 1:

    ...

   break;

case 2:

    ...

    break;

default:

    ...

}

 

SR-24

Indent nested control structures by a minimum of 2 ( 3 or 4 recommended) character positions.  E.g.

 

while ( ... )

{

   if ( ... )

   {

      ...;

      for ( ...; ...; ...)

         ...;

      ...;

   }

   ...;

}

 

Be consistent in your indentation.  E.g. Always indent in multiples of 3 spaces.

 

SR-25

if ( isalpha(ch) != 0 )     à if ( isalpha(ch) )          , or

                               if ( isalpha(ch) == true ) 

if ( isalpha(ch) == 0 )     à if ( !isalpha(ch) )         , or

                               if ( isalpha(ch) == false ) 

 

 

SR-26

Do not combine a declaration of a struct type with a definition of a variable of that type.   E.g.

 

struct EmployeeType

{

    char   name[MAX_SIZE_NAME+1];

    double salary;

} leadProgrammer;

 

à

 

struct EmployeeType

{

    char   name[MAX_SIZE_NAME+1];

    double salary;

};

 

EmployeeType leadProgrammer;

 

 

SR-27

Avoid anonymous (unnamed) struct types.   E.g.

struct

{

    char   name[MAX_SIZE_NAME+1];

    double salary;

} leadProgrammer;

 

 

à

 

struct EmployeeType

{

    char   name[MAX_SIZE_NAME+1];

    double salary;

};

 

EmployeeType leadProgrammer;