Skip to main content.
Computer Science Home > Resources > C++ Program Style and Documentation Guidelines

SUMMARY OF C++ PROGRAMMING STYLE GUIDELINES


  • GENERAL REQUIREMENTS

    1. All program units shall contain initial documentation as outlined in this document.

    2. Each statement must begin on a separate line.

    3. No program unit should exceed two pages in length (excluding documentation).

    4. Functions should be separated by at least two blank lines or other full-line separator.

    5. Each line should have no more than 80 characters.

    6. Avoid global references.

    7. The contents of all code blocks should be indented 3-5 spaces from the left margin of the enclosing level.

    8. Braces should be placed consistently to show the level of indentation/nesting.

    9. Use constants instead of literal values wherever possible.

  • DECLARATIONS

    1. All scalars (numbers and characters) that remain constant throughout a program must be specified in a constant declaration.

    2. All types should be named. Use type definitions (typedef) as needed and illustrated in class.

    3. Each variable must be declared on a separate line with appropriate inline commentary.

    4. Each field in a struct or union should be declared on a separate line with appropriate inline commentary.

    5. Include function prototypes in each non-recursive caller.

  • CONTROL STRUCTURES

    1. goto 's are not allowed.

    2. Nesting of any combination of if, for, while, switch, and do {...} while statements must be no more than four levels deep.

    3. Null statements must include a comment line, for example,

      ; // do nothing


    4. if statements are displayed in a comb-like fashion rather than a nested fashion. Individual paths must be indented and enclosed in braces.

    5. The bodies of {...} blocks, for, do{...}while, while, and switch statements, must be indented from their corresponding headers.

    6. for statements should not be prematurely terminated by break, exit, or return statements.

    7. for loop control variables should not be modified other than the necessary step increment/decrement.

    8. for loop control parameters like step-size and termination value should not be modified during the execution of the loop.

    9. All control statements should be followed by an indented code block enclosed with braces, even if it only contains one statement.

    10. switch Statements: Specify a break statement after every block.

    11. switch Statements: Specify a default case.

  • SPACING

    1. Uniform and constant indentation is to be used.

    2. A space should be used as in normal English usage.

  • 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

    1. 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



    2. Every class will contain a default constructor that initializes each data member to a known state.

    3. Every class will contain a copy constructor.

    4. Every class should contain a destructor, as illustrated in class.

    5. Every class will define the assignment (=) operator.

    6. Maximum level of inheritance should be three (3).

    7. Member functions that do not modify the object should be declared const.


PROGRAM DOCUMENTATION GUIDELINES:

/*===============================================================

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.