Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method comments and annotations... where should each go?

So, lets say I have a method that contains an annotation like so:

@Override
public void bar(String x)

If I were to add Javadoc comments to this snippet of code, which is the preferred method?

Either:

/**
* @param x A string lol
*/
@Override
public void bar(String x)

Or:

@Override
/**
* @param x A string lol
*/
public void bar(String x)
like image 735
Polaris878 Avatar asked Dec 07 '10 17:12

Polaris878


People also ask

Does javadoc 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.

Do Javadocs go before or after imports?

javadoc is "inherited" - javadoc written for an interface method or an abstract method is automatically associated with corresponding concrete implementations. it's a common error to put the class level javadoc comment at the very start of a source file, before the import statements.

When should annotations be used?

By annotating a text, you will ensure that you understand what is happening in a text after you've read it. As you annotate, you should note the author's main points, shifts in the message or perspective of the text, key areas of focus, and your own thoughts as you read.

How do you do method annotations?

reflect. Method. getAnnotation(Class< T > annotationClass) method of Method class returns Method objects's annotation for the specified type passed as parameter if such an annotation is present, else null. This is important method to get annotation for Method object.


4 Answers

First one. The annotation applies to the method, not the comment. It's also what most IDEs will do, so is the most common anyway.

like image 56
OrangeDog Avatar answered Oct 27 '22 04:10

OrangeDog


Personally, I prefer the former (i.e. annotation "touching" the method signature), since then it's code with code.

But either works for the compiler, so it's down to personal taste/your organisation's coding standards.

like image 40
Andrzej Doyle Avatar answered Oct 27 '22 04:10

Andrzej Doyle


Opinion: The first method is preferable. In a way the annotation and the method belongs together stronger than the comment.

like image 37
Vincent Ramdhanie Avatar answered Oct 27 '22 06:10

Vincent Ramdhanie


Generally annotations are pit on the line (or lines) immediately before the method. Annotations can be a bit long to put on the same line.

However, @Override is a bit special. It's effectively making up for the language not having override. Conventionally it is placed on the same line (although you'll see plenty of examples where it isn't).

like image 29
Tom Hawtin - tackline Avatar answered Oct 27 '22 04:10

Tom Hawtin - tackline