3

I really like well documented code. But there are lots of duplications in the documentation, since the basic information and examples should be available for the properties, the setters/getters and in constructors (also see Simple Getter/Setter comments).

Is there a way to avoid duplication of JavaDocs? The only idea I had is using {@link #function()} and link to a part in the JavaDocs that contains more information.

Here is an imaginary example:

package com.stackoverflow.tests;

/**
 * An Event ...
 */
public class Event {

  /**
   * The date the event takes place.
   * Needs to be in <a href="https://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a> format.
   * 
   * <h2>Examples:</h2>
   * <ul>
   *   <li>{@code 2016-03-19}</li>
   *   <li>{@code 2016-03-19T05:54:01+00:00}</li>
   *   <li>{@code 2016-W11} - i.e. week 11 of 2016</li>
   * </ul>
   */
  private String date;

  /**
   * Creates an event initializing it with the date and location the event takes place.
   * @param date
   *   Date in <a href="https://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a> format. Examples:
   *   <ul>
   *     <li>{@code 2016-03-19}</li>
   *     <li>{@code 2016-03-19T05:54:01+00:00}</li>
   *     <li>{@code 2016-W11} - i.e. week 11 of 2016</li>
   *   </ul>
   * @param location
   *   Location ...
   */
  public Event(final String date, final String location) {
    this.date = date;
  }

  /**
   * The date the event takes place.
   * @return
   *   Date in <a href="https://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a> format.
   */
  public String getDate() {
    return date;
  }

  /**
   * Updates the date the event takes place using an ISO 8601 formatted String.
   * @param date
   *   Date in <a href="https://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a> format. Examples:
   *   <ul>
   *     <li>{@code 2016-03-19}</li>
   *     <li>{@code 2016-03-19T05:54:01+00:00}</li>
   *     <li>{@code 2016-W11} - i.e. week 11 of 2016</li>
   *   </ul>
   */
  public void setDate(String date) {
    this.date = date;
  }

}
Community
  • 1
  • 1
Edward
  • 4,453
  • 8
  • 44
  • 82
  • do private members having self-talking names really need that detailed description, especially in bean class? – Alex Salauyou Apr 01 '16 at 12:01
  • @SashaSalauyou: Well, maybe this is a bad example. But generally I think it's a good idea to add documentation to private member variables. It really helps when working on the code (Eclipse e.g. shows the docs when moving the mouse over a documented method). – Edward Apr 01 '16 at 12:05
  • If you are worrying about this, your classes must have many getter/setter pairs. [It can be argued](http://stackoverflow.com/a/12108025/545127) that suggests you have a poor class design. – Raedwald Apr 01 '16 at 12:49

1 Answers1

3

@link is an option, but I would prefer to use @see annotation:

@see "string"

Adds a text entry for string. No link is generated. The string is a book or other reference to information not available by URL. The Javadoc tool distinguishes this from the previous cases by looking for a double-quote (") as the first character.

@see <a href="URL#value">label</a>

Adds a link as defined by URL#value. The URL#value is a relative or absolute URL. The Javadoc tool distinguishes this from other cases by looking for a less-than symbol (<) as the first character.


I think you will find this one specially useful, you can link to standard setters/getters comments and avoid duplicated information.

@see  package.class#member  label

Adds a link, with visible text label, that points to the documentation for the specified name in the Java Language that is referenced.

Typical forms for @see package.class#member
Referencing a member of the current class
@see  #field
@see  #method(Type, Type,...)
@see  #method(Type argname, Type argname,...)
@see  #constructor(Type, Type,...)
@see  #constructor(Type argname, Type argname,...)

Referencing another class in the current or imported packages
@see  Class#field
@see  Class#method(Type, Type,...)
@see  Class#method(Type argname, Type argname,...)
@see  Class#constructor(Type, Type,...)
@see  Class#constructor(Type argname, Type argname,...)
@see  Class.NestedClass
@see  Class

Referencing an element in another package (fully qualified)
@see  package.Class#field
@see  package.Class#method(Type, Type,...)
@see  package.Class#method(Type argname, Type argname,...)
@see  package.Class#constructor(Type, Type,...)
@see  package.Class#constructor(Type argname, Type argname,...)
@see  package.Class.NestedClass
@see  package.Class
@see  package
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • Probably one really has to copy and paste the description for parameters. This is really annoying, since the description does not change (For example when having 5 constructors all taking `date` as a parameter). One thing I really don't like using `@see` is the fact, that links are added to the end of the docs. And when using `@link` you still have to click to get more information. – Edward Apr 01 '16 at 12:10
  • Edward... And configure your IDE templates to auto-write javadoc wont fit your needs? – Jordi Castilla Apr 02 '16 at 07:28