DigitalZebra
DigitalZebra

Reputation: 41533

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)

Upvotes: 10

Views: 5069

Answers (4)

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147164

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

Upvotes: 1

Vincent Ramdhanie
Vincent Ramdhanie

Reputation: 103135

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

Upvotes: 2

Andrzej Doyle
Andrzej Doyle

Reputation: 103807

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.

Upvotes: 3

OrangeDog
OrangeDog

Reputation: 38777

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.

Upvotes: 16

Related Questions