CSC 102 Java Coding Conventions
except where the remaining conventions in this document deviate for the Sun conventions.http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html,
import ... ; /**** * * The comment at the top of a .java file is a high-level description of the * class defined in the file. Start the description with the words "Class X" * and then describe the purpose of the class and the major methods it * provides. The method descriptions in this header comment are generally * brief, e.g., "Class X provides methods to add, delete, change, and find * its elements." Do not list all of the method details in the header * comment, since full comments for each method appear below in the body of * the class, at the site of each method declaration. The header comment can * describe the data representation used in the class in high-level terms if * it's germane to explaining what the class is for. The header comment does * not describe low-level details of the data representation or any details of * method implementation. * * * @author Name and current email address of file's author; name is at * least first and last, with middle name or initial if necessary or * commonly used by author; email address appears in parentheses * following full name; email address may be abbreviated to a local * address if the full address can be expected to be known. E.g., * Gene Fisher (gfisher@vogon) * */ [public] class X extends Y implements Z { /** * Prose description of the method, describing what the method does, * not how it is implemented. Describe the use of each parameter by name, * refer to the instance object as "this", and use the word "return" to * describe the return value if there is one. Also describe each data * field used as an input and each data field modified as an output. * Modification to a parameter or data field includes indirect * modification to the value of a reference parameter or data field. * Stylistically, use complete sentences, avoid passive voice. * * @param -- brief description of each parameter, one line per param * @returns -- brief description of return value, if non-void */ public T1 methodName(T2 t2, ..., Tn tn) [throws ...] { /* * Include comments for each local variable and comments above each * line or group of lines that describe how the method works. All * but the "totally obvious" lines of code should be commented. * Comments for variables should be descriptive noun phrases. Comments * for code lines should be in complete sentences. * * All code comments should be formatted exactly as as this one is: (1) * start with "/*" on a separate line, indented to the current level of * code indentation; (2) start each comment line with "*", indented to * current indentation + 1; (3) end with "*/" on a separate line, * indented to current indentation + 1. * * See below under "indentation and spacing conventions" for further * discussion of the format of code within a method body. */ } ... other public methods ... Note no public or private data fields Protected methods in same format as public methods. /** Comment describing data field */ protected T1 varname; ... other protected and private data fields ... };
T1 methodName(T2 t2, ..., Tn tn) { /* Comment ... */ Tv1 tv1; ... /** Comment ... */ Tvm tvm; /* * Comment ... . */ for (...) { /* * Comment ... . */ if (...) { ... } /* * Comment, if necessary. */ else if { ... } ... /* * Comment, if necessary. */ else { ... } } /* * Comment ... . */ while (...) { ... } /* * Comment ... . */ for (start-expression; while-expression; end-expression) { ... } /* * Comment ... . */ for (long-start-expression ... ; long-while-expression ... ; long-end-expression) { ... } /* * Comment ... . */ switch (...) { /* * Comment, if necessary. */ case c1: ... break; ... /* * Comment, if necessary. */ case ck: ... break; } }In the preceding commenting convention, the '//' style of comments can be used instead of the '/* ... */', but the two styles should not be mixed. For multi-line comments with '//', format as follows:
// // Comment ... //
the following is NOT legalfor (i=1; i<=n; i++) { if (i % 2) { x.Foo1(i); x.Foo2(i); } }
nor is even the followingfor (i=1; i<=n; i++) { if (i % 2) { x.Foo1(i); x.Foo2(i); } }
The following are exceptions to the 50-line rule:for (i=1; i<=n; i++) { if (i % 2) { x.Foo1(i); x.Foo2(i);} }
Scoring of 102 programs will be based on execution results and adherence to coding conventions. Points for program execution are defined in the test drivers for each program, on a 100-point scale.
After the execution score is determined for a program, points are deducted for
any violations of the conventions specified in this handout. The following are
the specific deductions: