Are there any conventions for how to order Java annotations?
Asked Answered
T

3

19

For projects heavily used of Java annotations, is there any suggestion on the annotation order? So as to uniform the code style in the team.

Tern answered 12/4, 2011 at 16:8 Comment(1)
alphabetical, or in reverse hex order =)Ngo
A
12

I'm going to say there's not. I could be proven wrong on this.

My reasoning is there are hundreds of projects that have annotations, in addition to the ones baked into Java, and the ones baked into our IDEs (specifically Eclipse but I'm sure others have them too.)

So, with all these libraries potentially competitng for "whose on top" I doubt they would agree on a standard.

I would encourage your team to sit down and make decisions about what's best for you guys.

Factors I would consider:

  1. Which annotations are most important? They should be near the top.
  2. Which annotations are most likely to be indicative of potential bugs? They should be near the top.
  3. Which annotations are likely to be on every method in the class? They should be near the bottom.
Atchley answered 12/4, 2011 at 16:14 Comment(3)
Personally I would add 0. Annotation qualifier comes first before the qualified annotation. So @Lazy would before @Service. Do you agree?Tern
I don't like putting a qualifier before the qualified annotation. When reading "Lazy" it's a lazy what? Service tells me I'm working with a service, and lazy then makes the service more specific. Yes, English uses adjectives before nouns, but object oriented programming (Java) puts objects before methods and fields. Something "under" the annotation (a qualifier to an annotation) might be seen as an element "under" the annotation, not before it. Like I said in my answer, get team agreement.Detwiler
Gotta hand it to Edwin there. I like his logic there, and his last sentence is more important than everything else in either of our posts (except where we say it in our posts.) The most important thing you can do is get input from your team. Edwin and I don't have to work in your code base. They do.Atchley
D
4

If you must impose an order, alphabetical?

Without some sort of knowledge about the annotations you are using, it's hard to suggest a rational ordering. Perhaps a better ordering would suit you and your team better; and if so, if you document what's the agreed order, the only thing left is to verify it follows the team agreed ordering.

After talking to your whole team, you might find you don't need to order them after all.

Detwiler answered 12/4, 2011 at 16:11 Comment(2)
FQCN-albhabetically? E.g. all com annotations before org. Seems interestingScamper
@usr-local-ΕΨΗΕΛΩΝ Well, it might not be the best plan; but, in absence of a plan, it would impose some ordering.Detwiler
C
4

Not an exact science but I try to arrange annotations from a "wide scope" at the top to a "narrow scope" at the bottom. Here's an example:

@RadioDomain
@Entity
@Table(name = "receiver")
@ReceiverConstraintCheck
@SuppressWarnings("PMD.ShortVariable")
public class Receiver {
    // ...
}
Coenosarc answered 16/1, 2015 at 13:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.