Javadoc Best Practice


What to Comment

  • Document Why, not just“What

The summary fragment

  • use the third person form
  • end with period.
  • describe what the method does, not how it does it
  • put implementation comments in the body
This is a fragment—a noun phrase or verb phrase, not a complete sentence. It does not begin with A {@code Foo} is a..., or This method returns..., nor does it form a complete imperative sentence like Save the record.. However, the fragment is capitalized and punctuated as if it were a complete sentence.

Tip: A common mistake is to write simple Javadoc in the form /** @return the customer ID */. This is incorrect, and should be changed to /** Returns the customer ID. */.

Where to Write Javadoc

  • for every public non-test class, and every public or protected member of such a class

Javadoc for class

  • ImmutableSet > A {@link Set} whose contents will never change, with many other important properties detailed at
  • {@link ImmutableCollection}.
Util class
  • what the members of the class are and what may have in the future
  • Futures > Static utility methods pertaining to the {@link Future} interface.

Params

  • Treat @param and @return as a phrase
    • They should start with a lower case letter, typically using the word “the”.
    • They should not end with a dot.
  • No need to document all params.
  • The @param entries should be specified in the same order as the parameters.
  • Use @param document type parameters when really needed: @param <T>
  • If null is accepted, make sure it’s clear what happens in that case.
    • which may be null.

@param elements the {@code Iterable} to add to the {@code ImmutableList}

throws

  • Treat @throws as an if clause
  • Futures.getDone(Future future)

Overriding

  • On implementation class when there is nothing to add
  • when want to add something:

Use minimal HTML tags, not valid XHTML

  • omit optional closing tags (</p>, </li>),
  • just <p>, not <p></p> or </p>
  • {@link} allows the IDE to provide a click-able link and to update it when Refactor > Rename is used.
  • {@link} shows warning when there is a typo.
  • Link to a class named ‘Foo’: {@link Foo}.
  • Link to a method ‘bar’ on a class named ‘Foo’: {@link Foo#bar}.
  • Link to a method ‘baz’ on this class: {@link #baz}.
  • Link specifying text of the hyperlink after a space: {@link Foo the Foo class}.
  • Link to a method handling method overload {@link Foo#bar(String,int)}.
  • Do not use @code for null, true or false
  • Don’t use <code> or <tt>, or @see
  • Use {@code Set} instances instead of {@code Set}s

More Elements

Deprecated

More Javadoc Annotations

  • @apiNote, @implSpec and @implNote
  • @author

Misc Tips about Javadoc

  • javadoc should be before Annotation.
  • put TODO comment out of the Javadoc since callers don’t need to know about.

Common Mistake when Write Javadoc

  • missing javadoc for non-private methods in non-test class
  • not using the 3rd person
  • not needed/redundant
  • typo
  • error caused by copy/paste
  • there should be trailing period in summary, no trailing period in params, returns and throws.
  • forget to update javadoc after change code logic or signature
  • extra asterisk in javadoc.
  • extra terminating period in @parmas or @returns, @throws.

Labels

adsense (5) Algorithm (69) Algorithm Series (35) Android (7) ANT (6) bat (8) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) Java (186) JavaScript (27) JSON (7) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) regex (5) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) xml (5)