[prev][next]
5- Comments
java programs can have two kinds of comments: implementation comments and
documentation comments. Implementation comments are those found in C++, which are
delimited by /*.....*/, and //. documentation comments (known as "doc comments") are
java-only, and are delimited by /**...**/. doc comments can be extracted to html files
using the javadoc tool.
implementation comments are means for commenting out code or for comments about the
particular implementation. doc comments are menat to describe the specification of the code,
from an implementation-free perspective to be read by developers who might not necessarily
have the source code at hand.
comments should be used to give overviews of code and provide additional information that is
not readily available in the code itself. comments should contain only information that is
relevant to reading and understanding the program. for example, information about how the
corresponding package is built or in what directory it resides should not be included as a
comment.
Discussion of nontrivial or noobvious design decisions is appropriate, but avoid duplication
information that is present in(and clear from) the code. it is too easy for redundant comments
to get out of date. in general, avoid any comments that are likely to get out of date as the code evolves.
note:The frequency of comments sometimes reflects poor quality of code. when you feel
compelled to add a comment, consider rewriting the code to make it clearer.
comments should not be enclosed in large boxes drawn with asterisks or other characters
comments should never include special characters such as form-feed and backspace.
5.1 Implementation comment Formats
Programs can have four styles of implementation comments; block, single-line, trailing and
end-of-line.
5.1.1 Block comments
Block comments are used to provide description of files, methods, data structures and
algorithms. block comments may be used at the begining of each file and before each
method. they can also be used in other places, such as within methods. block comments inside
a function or method should be indented to the same level as the code they describe
A block comment should be preceded by a blank line to set it apart form the rest of the code.
/*
*Here is a block comment.
*
*/
block comments can start with /*-, which is recognized by indent(1)as the beginning of a
block comment that should not be reformatted. Example:
/*-
*here is a block comment with some very special
*formatting that i want indent(1) to ignore.
*
* one
* two
* three
*
*/
note:if your dont use indent, you dont have to use /*- in your code or make any ohter
concessions to the possibility that someone else might run indent(1) on your code.
see also "Documentation Comments"on Page 8.
5.1.2 Single-Line Comment
Short comments can appear on a single line indented to the level of the code that follows. if a
comment cant be written in a single line, it should follow the block comment format(see
section 5.1.1). A single-line comment should be preceded by a blank line. heres an example
of a single-line comment in java code:
if (condition) {
/*Handle the condition. */
...
}
5.1.3 Trailing Comments
Very short comments can appear on the same line as the code they describe, but should be
shifted far enough to separate them from the statements. if more than none short comment
appear in a chunk of code, they should all be indented to the same tab setting.
Heres an example of a trailing comment in java code:
if (a == 2) {
return true; /*special case*/
}else {
return isprime(a); /*works only for odd a*/
}
5.1.4 End-of-Line Comments
The // comment delimiter can comment out a complete line or only a partial line. it shouldnt
be used consecutive multiple for text comments; however, it can be used in
consecutive multiple lines for commenting out sections of code. Examples of all three styles
follow:
if (foo>1) {
//do a double-flip;
...
}
else{
return false; //explain why here.
}
//if (bar >1) {
//
// //do a triple-flip.
// ...
//}
//else{
// return false;
//
//}
5.2 Documentation Comments
note:"Java source file example" on page 18 for examples of the comment formats
described here.
for further details, see "How to write doc comments for javadoc" which includes
information on the doc comment tags(@return,@param,@see):
http://java.sun.com/products/jdk/javadoc/writingdoccomments.html
for further details about doc comments and javadoc, see the javadoc home page at:
http://java.sun.com/products/jdk/javadoc/
Doc comments describe Java classes, interfaces, constructors, methods, and fields. each doc
comment is set inside the comment delimiters /**...*/, with one comment per class,
interface, or member. this comment should appear just before the decleration:
/**
* The Example class provides...
*/
public class Example { ...
NOtice that top-level classses and interfaces are not indented, while their members are. the first
line of doc comment (/**) for classes and interfaces is not indented; subsequent doc comment
lines each have 1 space of indentation (to vertically align the asterisk). Members, including
constructors, have 4 spaces for the first doc comment line and 5 spaces therafter.
If you need to give information about a class, interface, variable or method that isn't
appropriate for documentation, use an implementation block comment (see section 5.1.1) or
single-line (see section 5.1.2) comment immediately after the declaration. for example, details
about the implementation of a class should go in in such an implementation block comment
following the class statement, not in the class doc comment.
Doc Comments should not be positioned inside a method or constructor definition block,
because java associates documentation comments with the first decleration after the comment.