Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Javadoc comments be added to the implementation?

Is it correct practice to add Javadoc comments in the interface and add non-Javadoc comments in the implementation?

Most IDEs generate non-JavaDoc comments for implementations when you auto generate comments. Shouldn't the concrete method have the description?

like image 648
Vaishak Suresh Avatar asked Jun 17 '10 11:06

Vaishak Suresh


People also ask

Where do I put Javadoc comments?

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 Javadoc comments go before or after annotations?

Also of interest here - the annotation is on the same line as the other qualifiers for the method. I've never seen that done before, but it seems to suggest that annotations should be treated much like other qualifiers for a method, and as such, the javadoc should definitely go before it.

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.

Do interfaces need Javadoc comments?

Yes you should write proper Javadoc comments for your interfaces to clearly specific the motivation behind the interface and what the contract is for both callers and implementors.


2 Answers

For methods that are implementation only (not overrides), sure, why not, especially if they are public.

If you have an overriding situation and you are going to replicate any text, then definitely not. Replication is a surefire way of causing discrepancies. As a result, users would have a different understanding of your method based on whether they examine the method in the supertype or the subtype. Use @inheritDoc or don't provide a documentation - The IDEs will take the lowest available text to use in their Javadoc view.

As an aside, if your overriding version adds stuff to the documentation of the supertype, you could be in a world of trouble. I studied this problem during my PhD and found that in general folks will never be aware of the extra information in the overriding version if they are invoking through a supertype.

Addressing this problem was one of the major feature of the prototype tool that I built - Whenever you invoked a method, you got an indication if its target or any potential overriding targets contained important information (e.g., a conflicting behavior). For instance, when invoking put on a map, you were reminded that if your implementation is a TreeMap, your elements need to be comparable.

like image 66
Uri Avatar answered Sep 22 '22 01:09

Uri


Both the implementation and the interface should have javadoc. With some tools, you can inherit the documentation of the interface with the @inheritDoc keyword.

/**  * @inheritDoc  *  * This implementation is very slow when b equals 3.  */ public foo(int b) { ... } 
like image 27
Sjoerd Avatar answered Sep 22 '22 01:09

Sjoerd