Professional Documents
Culture Documents
• readable
• maintainable
• robust
• description
• block tags
Example
/**
* Returns an Image object that can then be painted on the screen.
* The url argument must specify an absolute {@link URL}. The name
* argument is a specifier that is relative to the url argument.
getImage
* <p> public Image getImage(URL url,
description * This method always returns immediately, whether or not the String name)
Returns an Image object that can then be painted on the
* image exists. When this applet attempts to draw the image on
screen. The url argument must specify an absolute URL. The
* the screen, the data will be loaded. The graphics primitives name argument is a specifier that is relative to the url
* that draw the image will incrementally paint on the screen. argument.
from http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html
Quick Guide to Style
• for class/interface/field descriptions omit the object and just state subject:
• avoid Latin:
• most useful:
• @author jimmyNail
• @version 1.6
• @param for each data type give the name (not type) and description
• @return just like @param, can omit for constructors and void returns
• problem:
• often the legal set of inputs for a method is smaller than the
possible set of inputs that a client can pass to it
• solution:
• this specifies the validity of the input from the method's perspective
• for squareRoot():
@param value the number whose square root will be returned
@requires value ≥ 0
• this lets the reader know that they should not pass a negative value
to this method
• @requires tells us what the legal input is for a method, but it doesn't tell us what will
happen if illegal input is passed
• but instead to minimise the need for @requires in the first place
• use crafted types for the input parameters which are always valid
• problem:
• solution:
• problem:
• solution:
• specify using the @throws tag, one for each checked exception and its cause
• if:
• example:
/**
* @throws NegativeNumberException if value < 0
*/
public double squareRoot( double value) {
// method body
}
4. Use Exceptions rather than Special Return Values
• sometimes the method cannot provide a valid return for the given input
• for example:
• a getIndex(String name) method which returns the position in the list for a
given name
• it's tempting to add code to getIndex() to return a special value when this happens:
• and then let the client handle this special return value
• the client code for handling special value(s) might not be obvious
as such, unless we add lots of code comments
• solution:
client: client:
int index = getIndex( name); try { int index = getIndex( name); }
if ( index == -1 ) catch ( NameNotInListException nnilEx )
{ // appropriate reaction goes here } { // appropriate reaction goes here }
• the method and client code will need code • the code is now largely self explanatory
comments to explain the special value and its handling • the handling of the exception is enforced
• the handling will not be enforced in the specification •now and in future
• might get forgotten in future re-use
Benefits of Standards
• increases maintainability
• increases reliability
• but where:
• javadoc:
• http://www.oracle.com/technetwork/java/javase/
documentation/index-137868.html