50

For functions that don't change the state of an instance, the javadoc comment for the method is often the same or very similar as the one for the @return-tag in the Java-API.

boolean Collection.isEmpty()

  • Returns true if this collection contains no elements.
  • Returns: true if this collection contains no elements

Now I am writing javadoc for many simple methods like getExpression() where I have the same problem. Should I do it like in the API or leave it out?

2
  • 2
    JDK-8229111 tracks a feature request to allow omitting the method summary. Commented Aug 5, 2019 at 19:35
  • 1
    @Marcono1234 Thanks for keeping me up to date! I feel honored to have a Stack Overflow question mentioned in a JDK enhancement request :-) Commented Aug 7, 2019 at 7:15

4 Answers 4

34

If you (like me) really don't like to violate DRY, then this is one of the most important lines of the javadoc ref:

It is possible to have a comment with only a tag section and no main description.

(@see http://docs.oracle.com/javase/7/docs/technotes/tools/solaris/javadoc.html#tagsection)

So it is perfectly valid (and working) for simple methods to write your javadoc like:

/**
* @return the name of the object
*/
public String getName();

So you could even write something like this:

/**
* @return the n-th element of the object
*
* @param n index of element to get
*/
public String get( int n );

Which is (after a little getting to know each other) more readable in source and better maintainable as the longer form which violates DRY.

3
  • 5
    For some reason, the Google style guide (google.github.io/styleguide/…, point 7.2) calls this 'a common mistake' and recommends omitting the @return annotation altogether and just using the description. This seems to be at odds with the Oracle recommendation. Commented Jan 7, 2018 at 18:42
  • 2
    That said, the they give for not using a tag-only Javadoc seems pretty valid: if you look at docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html , for example, then the list of methods at the beginning includes only the body text, which will be empty if the comment consists only of tags. Commented Jan 7, 2018 at 18:44
  • Regarding Google's style guide: When omitting the @return tag the javadoc command issues a warning. If you are using this approach commonly it might be difficult to spot actual problems in the console output. Though as pointed out by John Gowers, leaving the description empty is also not a choice because then there will be no summary in the "Method Summary" table. Commented Aug 2, 2019 at 14:08
34

From Oracle's recommendation How to Write Doc Comments for Javadoc Tool:

@return (reference page)

Omit @return for methods that return void and for constructors; include it for all other methods, even if its content is entirely redundant with the method description. Having an explicit @return tag makes it easier for someone to find the return value quickly. Whenever possible, supply return values for special cases (such as specifying the value returned when an out-of-bounds argument is supplied).

1
  • 1
    Could you add a link to that recommendation? Commented Apr 10, 2012 at 12:52
23

With javadoc 16 you may make use of the new combo {@return ...} tag in order "to avoid duplication of return information in simple situations".

/**
 * {@return the name of the object}
 */
public String getName();

Is equivalent to the (still supported) style:

/**
 * Returns the name of the object.
 *
 * @return the name of the object
 */
public String getName();

Find more details at https://bugs.openjdk.java.net/browse/JDK-8075778

7

Since Java 16 the recommended solution is to use the newly introduced inline {@return description} tag of the Standard Doclet:

/**
 * {@return the name} {@code null} if unknown.
 */
public String getName() {
    ...
}

This will generate a "Returns description." summary, followed by whatever you write behind it, and additionally uses the description for the description of the return value:
Javadoc screenshot

Note that the "Returns ..." sentence already ends with a period; you should not add an explicit one behind {@return }.

Not the answer you're looking for? Browse other questions tagged or ask your own question.