COMMENTS

[Contents] [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 mean for commenting out code or for comments about the particular implementation. Doc comments are meant 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 nonobvious design decisions is appropriate, but avoid duplicating 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 used to provide descriptions of files, methods, data structures and algorithms. Block comments may be used at the beginning 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.

Each line of the block comments show start with // (C++ - style comment). Example:

// 1. Normally SCREEN_NAME value form the request is used to determine
//    which page is been processed (the value is setup in LF_SCREEN class
//    as a hidden input field
// 2. Path ./serguei clears all database connections and all cashed data
// 3. File FIT.INI is used to read configuration

See also "Documentation Comments".

5.1.2 Single-Line Comments

Short comments can appear on a single line. If a comment can't be written in a single line, it should follow the block comment format (see section 5.1.1). Here's an example of a single-line comment in Java code (also see "Documentation Comments"):

if (condition) 
{

    //    This is some comment
}

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 one short comment appears in a chunk of code, they should all be indented to the same tab setting.

Here's an example of a trailing comment in Java code:

if (a==2)
{
  return TRUE;            // special case
}
else
{
  return isPrime(a);      // works only for odd case
}

[Contents] [Prev] [Next]