Style Sheet For C and C++

Original: Dan Hyde of Bucknell University
Modified: Xiannong Meng for CS2330

The idea with program style is to have a readable program. One that has a professional appearance. In this course, we require certain standards of style as described below.

1. Every program must have a header as follows:

// Name: Sally Brown
// Course: CS2330 
// Date: August 30, 1996 
// Last Update: January 8, 1997
// Problem statement: This C++ program computes the volume  
//                    of Snoopy's house. 
2. Use blank lines to separate logical sections.

3. Use spaces around '=' and and around operators and after commas and semicolons. For example:

     int weight, height;
     weight = 3.0 + height * 2.7;
4. Use comments to describe major sections of program or where something needs to be clarified.

5. For names of objects (variables) you will use lower case letters and capitalize the first letter of the second and succeeding words (called camelBack style, see Mercer page 30). The names of classes and functions should start with a upper case letter. For constants, the identifier should be all capital letters. For example:

    const float PI = 3.14159; 
    float letterGrade; 
    class Fun 
    { 
       // stuff to define class 
    }; 
6. Use descriptive object and class names which relate the program to the problem.

7. Indent if, for and do-while as shown:

if( weight > 200)
{         	
   cout << "Too Big!\n";			
}
else
{					
   cout << "Ok.\n"; 
}
 

for(i = 1; i <=  n; i ++)
{
   s =  s + i;	
   cout << "   Don't Panic!\n"; 
}


do
{ 
   i = i - 1;
} while (i > 0);
8. All functions must have a series of comments which state the intent and the pre and post conditions. A pre-condition is an sentence or two which states what must be true before the function is called. The post-condition states what is true after the function is called.
   // Intent: To sum the positive integers from 1 to n. 
   // Pre:    The variable n must have a value and n > 0. 
   // Post:   The function returns the sum from 1 to n. 
   int Sum(int n)
   { 
       // code for Sum
   }