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.
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;
Use consistent and conventional levels of indentation, especially with regard to structures like if statements, loops, functions, etc.
Line up inline comments when they appear to the right of executable code such as variable declaration statements.
Add blank lines above and below structures such as if statements, loops, functions, etc.
Tabs and blank lines should be used to separate the program into logical and functional parts.
system("pause");
statement just before the main function's return 0; statement as in:
system("pause");
return 0;
}// end of main
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