// StyleExample
package its.examples; // Internet Technology School packages
import its.gis.ITShape;
import its.util.Tree;
/**
* A class implementing Java Style Example.
* The purpose of the class is …
* How to use this class: ...
* Example:
* @version 0.1 September 10, 1996
* @author Jeff.Zhuk@JavaSchool.com
*/
public class StyleExample extends ITShape {
// class data members
private Tree[] dataTree; // array of objects
// .. More data …
// class methods with JavaDoc headers
/**
* getDataTree(int iIndex) method returns one element of array
* @param iIndex element index in the dataTree[] array
* @return dataTree if available, return null in other cases
*/
public Tree getDataTree(int iIndex) {
if(iIndex < 0 || dataTree == null || dataTree.length < iIndex) { // check first!
return null; // to prevent run-time exceptions
}
return dataTree[iIndex];
}
// more methods ...
} // end of StyleExample class
"The Elements of Java Style" book describes more style details.
It requires to provide full JavaDoc comments on all classes, methods, and interfaces.
Comments should include description, example of usage, parameters,
return value, and exception tags if needed.
Use blocks in if-else, switch, and all looping constructs, it makes alterations fail-safe.
All cases and defaults get break statements for the same reason.
It is preferable to use StringBuffer for strings that get modified instead of using s +=.
Use String s = "hello"; // instead of String s = new String("hello");.
Do not use "try-catch" blocks to prevent run-time exceptions, but always use a proper logics instead (see above).
Design Technology . .
Java Tools and Packages