- WHY COMMENT?
- WHAT IS JAVADOC?
- WHEN JAVADOC?
- HOW TO JAVADOC?
To tell WHY!
By writing comment, you can tell something like:
- Why we have so unintuitive code at here
- Why we cannot use best practice at here
- Metadata of: parameters, returned value and exceptions
- How API user should call your API
MyDate(int year, int month, int day) {
// given parameter is one-based, but we need zero-based value
// to play with legacy systems and Java API
this.month = month - 1;
}/**
* This method still returns null (not empty Optional)
* to represents 'does not exist', because some user
* still using Java7.
* @see <a href="https://our.domain/redmine/issue/12345">related discussion and its result</a>
*/
Foo getFoo();/**
* @param columnIndex
* one-based value to point specific column,
* zero to specify all columns, or
* negative value to use index from right
* (e.g. -1 is rightest column)
*/
Columns getColumns(int columnIndex);Comment is not the way to explain followings:
- what variables/classes/methods represent
- we prefer better naming instead
- nullness of arguments and returned value
- we prefer JSR305 annotations (
javax.annotation.@Nonnull,@Nullableand@CheckForNull) instead - then IDE and static analysis tool will find problem based on annotations
- we prefer JSR305 annotations (
Comment is not the way to explain followings:
- negativeness of arguments and returned value
- we prefer JSR305 annotations (
javax.annotation.@Nonnegative, and@CheckForSigned) instead
- we prefer JSR305 annotations (
- API will close given
@Closeableinstance or not- we prefer JSR305 annotations (
javax.annotation.@WillClose,@WillCloseWhenClosedand@WillNotClose) instead
- we prefer JSR305 annotations (
When you worry that comment is not the best way, remember:
- Comment is to explain WHY, not WHAT.
- Javadoc is API documentation, to explain API spec.
It is documentation comment for Java.
- Javadoc is documentation comment
- javadoc is a tool to generate API document (HTML) from documentation comment
- Documentation comment is comment which explains specification of API
/** ← we need two asterisks to start javadoc.
*
* We can explain spec of API here freely.
*/Before code.
If time permits, doc-first development is the best.
- Find unclear thing in API asap
- (refs: Teddy bear effect, Rubber duck debugging)
- Use your future time to make new things, not to explain existing something
- Stop remembering complex tacit knowledge, by stipulated document
- Let API user to start develop even before you finish your implementation
It's simple HTML, and will be HTML5 from Java9.
You can also use block tag like @see, and inline tag like {@code ...}.
Just write HTML like below. Note that HTML escape like > is necessary in text body.
/**
:
* <h3>Invertibility</h3>
* <p>The reverse operation <b>may</b> be a strict <i>inverse</i> ...
:
* <p>Getting a converter:
*
* <ul>
* <li>Use a provided converter implementation, such as ...
:
*/
@Beta
@GwtCompatible
public abstract class Converter<A, B> implements Function<A, B> {Visit Guava on GitHub to see full example.
@paramDescribes the parameter of documented method, constructor, class and interface.@returnDescribes the returned value from documented method.@seeAdds a reference including class, interface, method, field, and URL.
See official document for detail.
{@link target text}Adds hyperlink to text. It is useful to link to related class, interface, field or method.{@code text}Wraps texts by <code> HTML tag. Inner text does not need HTML escape.{@literal text}Applies HTML escape to text.
See official document for detail.