CPS120: 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. The code one programmer writes must be consistent with the code written by others in the same environment. 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. Use proper spacing around all operators. 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=amount+sum<endl;
    use
    cout < sum = amount + sum < endl;
  1. Use consistent and conventional levels of indentation, especially with regard to complex structures like if statements, loops, switch structures, etc.
  2. Line up inline comments when they appear to the right of executable code.
  3. Add blank lines above and below structures including loops, if statements, switch structures, and functions.
  4. Tabs and spacing should be used to separate the program into logical and functional parts.
  5. If your program uses functions include function prototypes above the main function and place the function definitions themselves below the main function.
  6. Declare variables at the beginning of the function in which they are used including for loop variables.
  7. Use perfect grammar throughout your code, especially when a user can view it. You should use punctuation as well, where appropriate.
  8. Make sure that spelling is perfect in all code that you submit. You can copy and paste your code into a word processor, like MS Word, and run a spell-check before you submit your program if you want to be certain of your spelling.
  9. 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.
  10. 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. The parentheses after the keyword, main, are technically optional in Visual C++, but should be used anyway. So you must use the main function header:
    int main( )
    and the statement
    return 0;
    should be the last statement in the main function.


// Pat Doe

// CPS 120
// Assignment1.cpp

// February 19, 2001
// Purpose - This program is used as an example of certain

// documentation and coding standards.

#include <iostream.h>

int main()

{

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

int i = 0; // loop variable

int favoriteNum = 0; // user's favorite number

// ********* obtaining input ***************

cout < "Enter your favorite number: ";

cin > favoriteNum; // obtaining user's favorite number


// ********* processing and output *********

for(i = 1; i <= 10; i++)
{

cout < endl < "The current value of i is " < i < "." < endl;

if (i == favoriteNum)// checking for favorite number
{

cout < "i is your favorite number!" < endl;

}

}

return 0;

} // end of main