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
Blank lines, tabs, spacing, etc. should be used to separate the program into logical or functional parts.
Type blank spaces around operators of all kinds.
Example: Instead of cout<<sum=amount+sum<<endl;
use cout
<< sum = amount + sum <<
endl;
Use consistent and conventional levels of indentation, especially with regard to complex structures like if statements, loops, switch statements, etc.
Line up inline comments when they appear to the right of executable code.
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.
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.
Function definitions must be placed below the main function. Each function's prototype must be placed above the main function.
Use perfect grammar throughout your code, especially when it can be viewed by a user. You should use punctuation as well, where appropriate.
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.
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.
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( )
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.
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.
Try to avoid using global variables. If you must, be sure to be able to justify each one.
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.
Documentation
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. |
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 .
Include inline comments with closing braces of function definitions.
For example,
}// end of main
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.
Each function should have a header block of comments with the following:
Explain all applicable preconditions.
Explain the postconditions.
Wyo Area Comp Sci Documentation & Coding Standards | Computer Science Using C++ | Mr. Minich's Wyo Home Page