Code Maintenance: Specification Standards For Class Design: Sunday, 4 November 12
Code Maintenance: Specification Standards For Class Design: Sunday, 4 November 12
Sunday, 4 November 12
Sunday, 4 November 12
readable
maintainable
robust
Sunday, 4 November 12
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.
description
* <p>
* This method always returns immediately, whether or not the
* image exists. When this applet attempts to draw the image on
* the screen, the data will be loaded. The graphics primitives
* that draw the image will incrementally paint on the screen.
*
* @param url an absolute URL giving the base location of the image
block tags
* @param name the location of the image, relative to the url argument
* @return
* @see
*/
public Image getImage(URL url, String name) {
try {
getImage
publicImagegetImage(URLurl,
Stringname)
Returns an Image object that can then be painted on the
screen. The url argument must specify an absolute URL. The
name argument is a specifier that is relative to the url
argument.
This method always returns immediately, whether or not the
image exists. When this applet attempts to draw the image on
the screen, the data will be loaded. The graphics primitives that
draw the image will incrementally paint on the screen.
Parameters:
url - an absolute URL giving the base location of the image.
name - the location of the image, relative to the url argument.
Returns:
the image at the specified URL.
See Also:
Image
from http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html
Sunday, 4 November 12
Sunday, 4 November 12
Sunday, 4 November 12
for class/interface/field descriptions omit the object and just state subject:
avoid Latin:
Sunday, 4 November 12
@author
@version
classes and interfaces only
@param
@return
methods only
@throws
methods only
Sunday, 4 November 12
@author
@version
@author jimmyNail
of java for which this was written
@param
@version 1.6
for each data type give the name (not type) and description
@param height
millimetres
@return
just like @param, can omit for constructors and void returns
@throws
Sunday, 4 November 12
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
for example:
a squareRoot( double value) method requires
an input value 0
solution:
@requires
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
Sunday, 4 November 12
@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
Sunday, 4 November 12
use crafted types for the input parameters which are always valid
Using Exceptions
problem:
solution:
Sunday, 4 November 12
Creating Exceptions
problem:
Sunday, 4 November 12
solution:
Example: NegativeNumberException
public class NegativeNumberException extends IllegalArgumentException
{
public NegativeNumberException( double value)
{
super( value);
}
}
public double SquareRoot( double value) throws NegativeNumberException
{
if ( value < 0 ) { throw new NegativeNumberException( value); }
...
}
Sunday, 4 November 12
Sunday, 4 November 12
Sunday, 4 November 12
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:
Sunday, 4 November 12
solution:
Example: getIndex()
Before
After
method:
public int getIndex( String name)
{
method:
public int getIndex( String name) throws
NameNotInListException
{
...
if ( !list.contains( name) ) { throw new
NameNotInListException( String name) };
}
client:
client:
int index = getIndex( name);
if ( index == -1 )
{
// appropriate reaction goes here }
Sunday, 4 November 12
Benefits of Standards
Sunday, 4 November 12
Benefits of Standards
Sunday, 4 November 12
increases reliability
reduces the likelihood of runtime errors
increases the likelihood of compilation errors
To conclude
but where:
Sunday, 4 November 12
javadoc:
Sunday, 4 November 12
http://www.oracle.com/technetwork/java/javase/
documentation/index-137868.html