SUMMARY OF C++ PROGRAMMING STYLE GUIDELINES
- GENERAL REQUIREMENTS
- All program units shall contain initial documentation as outlined in this document.
- Each statement must begin on a separate line.
- No program unit should exceed two pages in length (excluding documentation).
- Functions should be separated by at least two blank lines or other full-line separator.
- Each line should have no more than 80 characters.
- Avoid global references.
- The contents of all code blocks should be indented 3-5 spaces from the left margin of the enclosing level.
- Braces should be placed consistently to show the level of indentation/nesting.
- Use constants instead of literal values wherever possible.
- All program units shall contain initial documentation as outlined in this document.
- DECLARATIONS
- All scalars (numbers and characters) that remain constant throughout a program must be specified in a constant declaration.
- All types should be named. Use type definitions (typedef) as needed and illustrated in class.
- Each variable must be declared on a separate line with appropriate inline commentary.
- Each field in a struct or union should be declared on a separate line with appropriate inline commentary.
- Include function prototypes in each non-recursive caller.
- All scalars (numbers and characters) that remain constant throughout a program must be specified in a constant declaration.
- CONTROL STRUCTURES
- goto 's are not allowed.
- Nesting of any combination of if, for, while, switch, and do {...} while statements must be no more than four levels deep.
- Null statements must include a comment line, for example,
; // do nothing
- if statements are displayed in a comb-like fashion rather than a nested fashion. Individual paths must be indented and enclosed in braces.
- The bodies of {...} blocks, for, do{...}while, while, and switch statements, must be indented from their corresponding headers.
- for statements should not be prematurely terminated by break, exit, or return statements.
- for loop control variables should not be modified other than the necessary step increment/decrement.
- for loop control parameters like step-size and termination value should not be modified during the execution of the loop.
- All control statements should be followed by an indented code block enclosed with braces, even if it only contains one statement.
- switch Statements: Specify a break statement after every block.
- switch Statements: Specify a default case.
- goto 's are not allowed.
- SPACING
- Uniform and constant indentation is to be used.
- A space should be used as in normal English usage.
- Uniform and constant indentation is to be used.
- SUBPROGRAM
- A function with a non-void return type may not change the values of its arguments.
- Array arguments that will not be modified must be passed by const reference.
- CLASSES
- Every class will be organized as follows:
#ifndef MeaningfulNameH
#define MeaningfulNameH
class MeaningfulName {
friend declarations
public methods
protected methods
private methods
protected data members
}
class method definitions or #include classdefinitionfile.cpp
#endif
- Every class will contain a default constructor that initializes each data member to a known state.
- Every class will contain a copy constructor.
- Every class should contain a destructor, as illustrated in class.
- Every class will define the assignment (=) operator.
- Maximum level of inheritance should be three (3).
- Member functions that do not modify the object should be declared const.
- Every class will be organized as follows:
/*===============================================================
MAIN PROGRAM DOCUMENTATION
===============================================================*/
// #include statements with in-line comments for non-standard libraries
// banner documentation consisting of:
// Assignment Number / Program name:
// Author:
// Class:
// Email Address:
// Language: C++
// Purpose:
// Description:
int main ( )
{
// Function Prototypes:
// Document each parameter with its purpose. In general, the
// following form will facilitate this:
-
return_type function_name (float param1 , // comment
int param2 , // comment
char param3); // comment
// Program Variable Declarations, with in-line comments giving a
// description of each variable.
// Program Body:
// Include comments to divide major
// sections, and where needed to explain the code.
} // END program_name
// Function Definitions:
// Document each parameter with its purpose. In general, the
// following form will facilitate this:
-
return_type function_name (float param1 , // comment
int param2 , // comment
char param3) // comment
{
// Purpose:.....
// Declaration of Variables, if any, with in-line comments.
// In the function body, include comments where
// necessary to explain code.
} // END function_name
/*================================================================
CLASS DOCUMENTATION GUIDE (class header)
================================================================*/
class class_name {
// banner documentation consisting of:
// Title:
// Author:
// Email Address:
// Language:
// Purpose:
// Method Specifications:
// Document each parameter with its purpose. In general, the
// following form will facilitate this:
-
return_type method_name(float param1 , //comment
int param2 , // comment
char param3); // comment
// Purpose:.......
} // END class_name
/*=============================================================
CLASS BODY DOCUMENTATION AND STYLE GUIDE
Follow guidelines for non-class functions. Parameter documentation may
be omitted if it is contained in the class header.
