Wyo C++ Program Coding Standards

Use the following coding styles and standards in your programs for AP/Honors Computer Science Using C++. You may lose points on any assignment if you fail to adhere to these guidelines.

Coding

  1. Blank lines, tabs, spacing, etc. should be used to separate the program into logical or functional parts.

  2. Type blank spaces around operators of all kinds.

    Example: Instead of     cout<<sum=amount+sum<<endl;
        use    cout  <<  sum  =   amount  +  sum  <<  endl;

  3. Use consistent and conventional levels of indentation, especially with regard to complex structures like if statements, loops, switch statements, etc.

  4. Line up inline comments when they appear to the right of executable code.

  5. Add blank lines above and below structures such as loops, multiple line if statements, and switch structures and blank lines should appear above and below signpost comments.

  6. Variable declarations must be placed at the top of a function. You must only place one variable declaration on each line so that you have room for an inline comment explaining the purpose of that variable.

  7. Function definitions must be placed below the main function. Each function's prototype must be placed above the main function.

  8. Use perfect grammar throughout your code, especially when it can be viewed by a user. You should use punctuation as well, where appropriate.

  9. Make sure that spelling is perfect in all code that you submit. If you have to copy and paste your code into a word processor, like MS Word, and run a spell-check before you submit your program.

  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.

  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. The parentheses after the keyword, main, are technically optional in Visual C++, but should be used anyway. So the function definition line for all main functions should match the following example:

    int main( )

  12. Always pseudocode and write out the code to programming assignments before typing the code into the computer. Failure to do so may result in a deduction of points.

  13. Always create a test plan before typing the code into the computer. Failure to do so may result in a deduction of points. Make sure that the test plan contains at least 5 test items that include boundary cases, average cases, out-of-range cases, and any other "interesting" cases. See this test plan as an example, although note that it does not include any out-of-range cases.

  14. Try to avoid using global variables. If you must, be sure to be able to justify each one.

  15. Functions should have no side effects. Parameters should be value parameters when possible but if you can justify passing by address or reference, then feel free to do so. A function should not contain input/output unless that is its specific purpose.

  16. Only one statement should appear on each line of code.
  17. Use Courier or New Courier font for all printed source code.

Documentation

  1. Every program should have a header at the beginning with the following information:

    // John Doe
    // Ch1Proj1
    // Period 5
    // Sept. 5, 2001
    // Purpose - to compute the sales tax on a set of purchases
    //                  a display the total price.

  2. 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.
  3. Always include a "variable dictionary." A variable dictionary is a set of inline comments in the right margin where each inline comment explains the purpose of the variable declared in that line of code.
  4. Variable and function identifiers should begin with a lowercase letter. Additional words or parts of words within an identifier should begin with a capital letter (e.g. numStudents, yearlySalary). All pointer variables must begin with a lower-case 'p'. All global variables must begin with a lower-case 'g'. Constant identifiers however should be typed in all capital letters with successive words separated by underscores (e.g. DEGREES_IN_RADIAN, PI, MAX_SIZE). Programs must be "self-documenting."  A program is self-documenting if its variable names imply the purposes of the variables. Variable names should be whole words or phrases that make refer to the purpose they serve within the program or function. For example, use variable names like lengthName rather than len .

  5. Include inline comments with closing braces of function definitions. For example,

    }// end of main

  6. Annotate medium to complex algorithms and assignment statements with inline comments. Do not necessarily assume that fellow programmers (or teachers) will understand your logic. However, do not document the obvious.

  7. Each function should have a header block of comments with the following:


Wyo Area Comp Sci Documentation & Coding Standards | Computer Science Using C++ | Mr. Minich's Wyo Home Page