Coding standards, including formatting and documentation are important because the human reader is one of the most important users of program source.
These rules have been set up to create the most clarity while reducing redundant documentation as much as possible.
Corrections and suggestions are invited.
/******************************************************************** Class: CSCI 470-1 Program: Assignment 1 Author: Your Name Z-number: z1234567 Date Due: mm/dd/yy Purpose: A short indication of what the program does. Execution: Command to execute your program (should match directions on assignment sheet) java hw1.java nnn If parm is omitted, 1000 is used. Notes: (optional) any special notes to your T.A. or other readers *********************************************************************/
avgHeight
.
avgHeight
.avgHeight
rather
than h
or hgt
.height
is preferable to hgt
.
Abbreviations can be useful for long or frequently repeated
words.
avg_height
for average height, then you should use
avg_weight
for average weight and not ave_
or average_
.
for (i = 0; i < 10; i++) ...
countLines()
.// some comment // more comments // even more comments
This form is also acceptable and can be more convenient for doc boxes at the top of a program or function.
/* some comment more comments even more comments */
The following examples show the size of chunk that should be commented.
// Loop to accept and process user-supplied glucose measurements // Decide if gizmos or widgets are to be used // Calculate and store averages and standard deviations of measurements
Some programs are longer or more complex than others. That means that main() may have several sections while a function that only does one thing may have only one. Occasionally, for a complex formula, a section will be only one line long:
If you find yourself frequently needing such comments, you should consider changing your nesting style.... } // end I/O loop
// Item subtotals int totItems; float totCost;
Horizontal alignment of variable names, as in the above examples, is encouraged but not required.
The following method is recommended:
if (condition) { statement1; statement2; } else { statement3; statement4; } while (condition) { statement 1; statement 2; }
If you use method 1, indent two spaces as shown.
Methods 2 and 3 are less desirable since they do not show indentation as clearly. If you use methods 2 or 3, two spaces are preferred; three or four are acceptable.
if (condition) { statement1; statement2; } else { statement3; statement4; } while (condition) { statement 1; statement 2; }
if (condition) { statement1; statement2; } else { statement3; statement4; } while (condition) { statement 1; statement 2; }
Other brace styles put the opening brace on the same line as the
if, else or while.
These are more difficult to read because opening and closing braces
do not line up.
These are permitted if you are used to them and prefer them.
Anyone who thinks that their preferred style is the only style or only logical style are encouraged to consult an exhaustive list of possible brace styles before making this assertion.
int myFn(longName1, longName2, ..., longName99)
Similarly, arithmetic expressions, I/O statements and other statements that overflow should aligned under the preceding arithmetic symbol (+, etc.), a repeated function call, or a similar item..
Spaces are better than tabs for indenting because they will display correctly in any configuration.
Any good editor will have a setting that prevents spaces from being converted to tabs.
If you use tabs, you are responsible for making sure your program will display correctly on turing/hopper. Unix defaults to tabs every 8 characters.
for (i = 0; i < 10; i++)
/**************************************************************** Class_name Author: Your Name Z-number: z1234567 Description: purpose of the class (optional) Any other information needed by the user ****************************************************************/
/*************************************************************** Getters for className ***************************************************************/
1
and 0
for
true
and false
.Break
may only be used to escape from a
while (true)
I/O loop. Although the use of
switch
is discouraged, break
may also be
used after each case of a switch
.The use of continue
is also discouraged.
Do not use any constructs that depend on a specific I/O
encoding. Therefore do not use ord
.
In general, do not turn in programs containing commented-out code, as it is confusing to the human reader.
The following good practices are encouraged because they simplify program debugging and updating. However, they are not required.
int avgHeight; int totalHeight;
is easier to update than
int avgHeight, totalHeight;
totalArea = (complex formula) return totalArea
is easier to debug than
return (complex formula)
if
or while
statements more than three deep./* ... */
format for multiline
comments, put the closing */
in column 1 of the line
following the comment. If you use lines of *'s to set off
sections of code, they should be the same length.
Don't use */
to end every line
inside a box, since it is difficult to keep them lined up.
if
or
while
is only a single line.