Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code style with Annotations [closed]

I can't make up my mind between

@MyAnnotation(param1="paramval")
public void foo(){}

and

@MyAnnotation(param1="paramval") public void foo(){}

Is there a best-practice emerging?

like image 499
Paul McKenzie Avatar asked Sep 23 '09 07:09

Paul McKenzie


People also ask

What is the use of @interface annotation?

The @interface element is used to declare an annotation.

How do I open annotations in IntelliJ?

Enable annotationsRight-click the gutter in the editor or in the Differences Viewer and select Annotate with Git Blame from the context menu. You can assign a custom shortcut to the Annotate command: go to the Keymap page of the IDE settings Ctrl+Alt+S and look for Version Control Systems | Git | Annotate.

What is Java code conventions?

The Java code conventions are defined by Oracle in the coding conventions document. In short, these conventions ask the user to use camel case when defining classes, methods, or variables. Classes start with a capital letter and should be nouns, like CalendarDialogView .

What are annotations eclipse?

Using AnnotationsAnnotations in Java 5 can be applied to package and type declarations (classes, interfaces, enums, and annotations), constructors, methods, fields, parameters, and variables. Annotations are specified in the program source by using the @ symbol.


2 Answers

We use the first case.

Annotations don't fit on one line in some cases.

  • What happens in that annotations keep adding up in our project, responsibility after responsibility. Having annotations for really different concerns on the same line becomes messy.
  • Also, some annotation can become really big, and be multi-liners on their own (I think of Hibernate mapping redefinition in a subclass).
like image 113
KLE Avatar answered Sep 21 '22 13:09

KLE


Annotations can have parameters, it can become very long if you put the annotation plus its parameters plus the method header all on one line.

@MyAnnotation(name = "This is the name", version = "1.0")
public void foo () {
    // ...
}
like image 43
Jesper Avatar answered Sep 19 '22 13:09

Jesper