Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javadoc comments vs block comments?

Tags:

java

comments

When is it appropriate to use a block comment at the beginning of methods, and when is it appropriate to use a javadoc-style comment?

From the "Comments" section of the Java style guide, I found this:

Java programs can have two kinds of comments: implementation comments and documentation comments. Implementation comments are those found in C++, which are delimited by /*...*/, and //. Documentation comments (known as "doc comments") are Java-only, and are delimited by /**...*/. Doc comments can be extracted to HTML files using the javadoc tool.

Implementation comments are meant for commenting out code or for comments about the particular implementation. Doc comments are meant to describe the specification of the code, from an implementation-free perspective. to be read by developers who might not necessarily have the source code at hand.

So, another way to phrase my question would be: When do methods deserve a specification of the code, from an implementation-free perspective (Javadoc) instead of a comment about a particular implementation, and vice versa? Would an interface get javadoc comments, while the implementations get block comments?

edit: I think I am not conveying my question correctly, based on the answers thus far.

Here's an example of what I want to know.

/**  * Javadoc comment here about general implementation?  */ /*  * Should I now have a separate block comment for my specific implementation?  */ public void foo() { ... } 

The two different comment styles convey two different types of information. Are there cases when methods should have BOTH a leading javadoc comment, and a leading block comment?

The inspiration for even asking is that Eclipse auto-generated this for me just now:

/*  * (non-Javadoc)  * @see my.package#process()  */ 

And I figured there is some sort of styling going on here that isn't declared specifically in the comment specifications I link to above.

like image 937
Tony Stark Avatar asked Aug 31 '10 09:08

Tony Stark


People also ask

What is the difference between Javadoc and comments?

Javadoc comments are more focused on the parameters of the methods, what your method will return depending on the parameters you give to your methods. Block comments are internal comments, comments you write for people maintaining your code.

When should you use Javadoc comments?

Normal Javadoc comments can be placed before any class, field, or method declaration to describe its intent or characteristics. For example, the following simple Student class has several Javadoc comments.

What are Javadoc comments?

The special comments in the Java source code that are delimited by the /** ... */ delimiters. These comments are processed by the Javadoc tool to generate the API docs. The JDK tool that generates API documentation from documentation comments.

What is the difference between multi-line comment and documentation comment?

Use /* text */ when you want to comment multiple lines of code. Use /** documentation */ when you would want to add some info about the program that can be used for automatic generation of program documentation.


1 Answers

Info that the user of a class needs to know should go into a Javadoc comment.

Info that a developer modifying a class needs to know go into a normal comment (block or line).

And it's very possible that any block of code (class, interface, field, method, constructor, ...) can have both a Javadoc comment and a normal comment block, when both publicly visible as well as internal documentaton is required.

Personally I tend towards writing very little non-Javadoc comments, because I prefer to structure my code in a way that it's self-documenting.

like image 141
Joachim Sauer Avatar answered Sep 17 '22 18:09

Joachim Sauer