Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javadoc @return tag comment duplication necessary?

Tags:

java

javadoc

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?

like image 475
Konrad Höffner Avatar asked Apr 10 '12 11:04

Konrad Höffner


People also ask

What should be included in a Javadoc comment?

Writing Javadoc Comments In general, Javadoc comments are any multi-line comments (" /** ... */ ") that are placed before class, field, or method declarations. They must begin with a slash and two stars, and they can include special tags to describe characteristics like method parameters or return values.

Do you need Javadoc comments for private methods?

Nope, you shouldn't write javadoc for private methods. End users don't have access to private fields or methods so there really isn't a point in providing javadoc for them. Private fields and methods are only meant for the developer. If you really need to though, feel free to write comments for non-obvious logic.

What is the purpose of having Javadoc specific comments within code?

The javadoc Tags Adds the author of a class. Displays text in code font without interpreting the text as HTML markup or nested javadoc tags. Represents the relative path to the generated document's root directory from any generated page. Adds a comment indicating that this API should no longer be used.


3 Answers

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.

like image 165
Scheintod Avatar answered Sep 19 '22 15:09

Scheintod


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).

like image 42
Alex Stybaev Avatar answered Sep 17 '22 15:09

Alex Stybaev


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

like image 28
Sormuras Avatar answered Sep 20 '22 15:09

Sormuras