Coding Standards

Study this program or practically any of the demo programs which adhere to the Coding Standards

When a programmer works or studies in an institution, he or she must work closely with others. Often, programmers work on team assignments. Therefore, the code that one programmer writes must be consistent with the code written by others around him or her. Following the institution's coding standards is essential. Therefore you must follow these guidelines for every program assignment even if examples in the textbook or from the lecture notes are not consistent.

  1. Type blank spaces around operators of all kinds. Blank spaces should be used to improve the readability of the program. Always surround binary operators such as assignment operator, plus symbols, etc. with blank spaces.

    Example:

    Instead of    cout<<sum<<endl;
        use        cout << sum << endl;
    Instead of    totalBookPrice=numBooks*PRICE_PER_BOOK;    use      totalBookPrice = numBooks * PRICE_PER_BOOK;  

  2. Use consistent and conventional levels of indentation, especially with regard to structures like if statements, loops, functions, etc.

  3. Line up inline comments when they appear to the right of executable code such as variable declaration statements.

  4. Add blank lines above and below structures such as if statements, loops, functions, etc.

  5. Tabs and blank lines should be used to separate the program into logical and functional parts.

  6. If your program uses functions include function prototypes above the main function and place the function definitions (i.e. the bodies of the functions) below the main function.

  7. Declare variables at the beginning of the function in which they are used. Do not declare multiple variables in the same declaration statement even though the compiler allows it. That is, do not use

    int numBooks, bookSubtotal;
    or
    int numBooks = 0, bookSubtotal = 0;

    Rather, you should use

    int numBooks = 0;       // quantity of books purchased by customer
    int bookSubtotal = 0;   // subtotal price of books purchased
          

  8. Use perfect grammar throughout your code, comments, and displayed output.

  9. Make sure that spelling is perfect throughout your code, comments, and displayed output.

  10. When you obtain user input, make sure that the input cursor stays on the same line as the output prompt (if possible and when appropriate.) It is best to use a colon (:) and a space to separate the input prompt from the user's actual input. For example, use a prompt statement like this:

    cout << "Enter number of books: ";
    cin >> numBooks;


    with no endl after the cout statement.

  11. The return type for the main function should be expressly set to int, even though int is the default return type. Make sure that zero is the value that is returned by the main function, too. That is, you must use the main function header:

    int main()

    and the statements

    system("pause");
    return 0;


    should be the last two statements in the main function.

  12. One-line if statements are not allowed. Also, the body of an if statement or each of its else if or else clauses must be placed in curly braces. The opening curly brace must be placed on the line below the keyword if and its control expression. For example,

    if (num > 0)
    {
       cout << num << " is greater than zero" << endl;
    }


    is proper. The following if statements do not follow these Coding Standards:

    if (num > 0) cout << num << " is greater than zero" << endl;

    or

    if (num > 0)
        cout << num << " is greater than zero" << endl;


    or

    if (num > 0) {
       cout << num << " is greater than zero" << endl;
    }


    or

    if (num > 0)
    { cout << num << " is greater than zero" << endl; }



  13. Curly braces must be used to bracket the bodies of for, while, and do while loops, even loops with only one body statement. The opening curly brace must be placed on the line after the loop's keyword and the closing curly brace should be placed on its own line of code except for do while loops. Follow these samples for proper style:

    for (i = 0; i < 10; i++)
    {
       cout << i << endl;
    }

    while (num < 10)
    {
       num++;
       cout << num << endl;
    }

    do
    {
       num++;
       cout << num << endl;
    } while (num < 10);

  14. Use Courier or New Courier font for all printed source code.

    Print your program from within the Visual C++ software. To avoid formatting mistakes, do not print your program from Angel or Microsoft Word.

  15. Do not add extra prompt messages or extra    system("pause");    commands (except for the one at the end of the program as described below) asking the user if he would like to continue or exit a program. Only add the exact prompt messages that are described in the assignment specifications. For example, if the program specs only call for allowing the user to type one input then do not prompt the user asking if he'd like to type a second input. Also, do not use a clear screen command ( such as    system("cls");    ) to clear the screen at any point during your program's execution.

  16. Be sure to add the

    system("pause");      

    statement just before the main function's return 0; statement as in:


       system("pause");
       return 0;
    }// end of main



Documentation Standards

All students must apply these principles to every program that is submitted for a grade. Even though a program may function properly, the programmer is responsible for documenting the program. Programming assignments will lose points if they do not follow good documentation principles.

1. Program header - Each program or procedure should have a header block of comments with the following comments:

// [Your name]
// [name of the assignment or filename]
// [day of the week that you class meets (e.g. Wednesday)]

2. Signpost Comments - If the module contains several major steps or functional parts, there should be comments (called signpost comments) explaining the function of each major part. The comment with the row of asterisks in the example below is an example of a signpost comment as in

// ****************** variable declarations **************************

3. Inline Comments - If a specific line or section of code is not self-explanatory, you must include an inline comment explaining the purpose of that line.  Do not necessarily assume that fellow programmers (or teachers) will understand your logic. In general it is better to "overdocument" than to "underdocument" but do not document the obvious. For example, the inline comment below helps others realize that the algorithm being used rounds to the nearest hundredth's place.

roundedPrice = int (totalPrice * 100 + 0.5) / 100.0;    // rounding to nearest penny

4. Variable Dictionary - A description of the purpose of each variable. This should be done with an inline comment on the line with the declaration of that variable. While this may take more room, it definitely enhances the readability of the program. This set of variable descriptions is sometimes called a variable dictionary. Do not declare multiple variables on the same line of code even though C++ allows this syntactically.

For example, the inline comments in the right margin serve as a variable dictionary. The purpose of each variable is explained. These comments must be lined up vertically.

// *********************************** variable declarations *******************
int apples = 0; 
double costApples = 0.0;
int i = 0;
const double PRICE_PER_APPLE = 0.50;
// number of apples purchased
// cost of apples purchased
// loop variable
// price per apple

 





Do NOT include comments such as:

int apples = 0;       // declaring apples as an integer and initializing it to zero

This comment states the obvious plus it does not explain the purpose of the variable apples. In fact, do not use any of the words "declare", "declaring", or "variable" in your comment.

5. Input and output prompts - Assume that the user is a "knucklehead". Certainly, the user cannot be expected to know anything about the computer programming language that you happen to be using. In fact, you should assume that the user knows little about computers in general. Therefore, you must always prompt the user with a clearly worded message to indicated EXACTLY what kind of input you expect him/her to enter. This prompt should be formatted nicely as well and it should be written with proper grammar. Likewise, you must include descriptive output prompts or messages with ALL output so that the user knows what each value represents. Note that input and output prompts are considered to be external documentation, not internal documentation.

6. Consistency - Use a consistent format with regard to internal documentation. If you use complete sentences in inline comments, then do so throughout your program's code, otherwise use explanatory, understandable phrases. Always use consistent indentation that improves the readability of the program. Each level of indentation must be either 2, 3 , or 4 spaces. You must be consistent. The indentation pattern must be consistently applied within the program. You must indent the body statements within ALL loops. You must indent each branch of a decision structure (eg. if/then statements). Inline comments, where used, must be aligned with each other as practically as possible. Variable declarations must be one per line and aligned consistently. Statements which are continued on a second line must be indented significantly (more than the normal indentation).

7. Variable Names - The names of your variables (aka identifiers) and functions must be self-documenting.  That is, they should be whole words or phrases that are meaningful and descriptive as to the purpose of the variable or function that they represent. In this course, identifiers must begin with a lowercase letter and successive words must be capitalized for readability. For example, numPassingStudents would correctly serve as a variable name for a variable that stores the number of students who pass this course. On the other hand, num_passing_students should not be used even though it is syntactically correct and would not cause a compiler error. Constant identifiers must be uppercase letters with the underscore operator used to separate multiple words. For example, PA_SALES_TAX_RATE would serve as a good constant identifier.

8. Include an inline comment after the closing brace of each function to mark the end of that function. For example, you should always document the closing brace of the main function like this,

} // end of main

If you have a function named round, then you should include a comment such as

}
// end of round