C Sci 40 Programming Guidelines Classes

The following two guides are so important that you will not receive credit on an assignment if they are not followed:

I.  Do not use global variables. The use of global constants is encouraged.

II.  Functions must follow the main program.  Function prototypes must precede the main program.

Violating these guides will cause you to lose points:

1.  Declare all variables at the beginning of a function.

2.  Every file needs an introductory comment stating the purpose of the file. Every function needs an introductory comment describing the purpose of the function. Additional comments are needed whenever the purpose of the code is not immediately clear.

3. Use the at member function to access characters in strings rather than direct indexing.

4.  Use meaningful variable names.

5.  If a program prints anything, the last thing printed should be endl or '\n'.

6.  Indent all compound statements.

7.  Any counter should be an ordinal data type (usually int or char; never float or double).

8.  Every function that returns a value should have exactly one return statement. That statement should be the last line of the function. Normally, if a function returns a value, it should not modify any of its parameters.

9.  Do not use ASCII codes in a program unless absolutely necessary.

10.  Do not terminate loops with break or continue. Break should only be used in a switch statement.  All conditions for terminating or continuing a loop should be given in the looping statement.

11.  In for loops that are counting loops, the beginning and ending values for the index variable should be explicitly stated in the for statement. This means using <= and >= instead of < and > in the condition for continuing the loop.

12.  If "MyCondition" is a boolean variable, never use:
if(MyCondition==true) or if(MyCondition==false)
always use:
if(MyCondition) or if(not MyCondition)

This means using variable names that specify what the situation is when the value of the variable is true.

13.  Use and, or, and not instead of &&, ||, and ! in boolean expressions.  (If you are using visual studio to compile, this may mean including the iso646.h header file.)

14.  Do not use the old C style of string (char arrays); use C++ strings.

15.  Do not use the old C style of reference parameters in function calls (passing pointers).  When passing an array to a function, make sure the first line of the function specifies the size of the array.

16.  When writing classes, always create separate header and implementation files.

17.  Make sure all linked lists are NULL terminated.