简体   繁体   中英

Annotation to replace in-line or in-code comments in Java

I have a requirement, I want to put in-line/in-code comment to describe a line of code. eg Using single liner comment

private foo(List myNameList){
    for(String name :  myNameList){

     //This prints each element of list
      System.out.println(name);
     }
} 

But many GREEN comments all over the code don't looks pretty.

I am just looking for an annotation or a solution to replace each comment with annotation eg

private foo(List myNameList){
    for(String name :  myNameList){

      @Comment(n)
      System.out.println(name);

     }
   } 

And just hovering over this @Comment should display my comment.

Note: In Comment(n) , n is an index of my messages/comments in some text file.

Don't use either.

If you think you need to write a comment explaining what a piece of code does, don't write a comment at all. Refactor the code. Extract out small, well-named methods that break the logic down into understandable pieces.

Inline comments in code should be rare, and provide information that cannot be gleaned by reading the code: for example, why something happens.

See: What is self-documenting code and can it replace well documented code?

Assuming you're an Eclipse user (hence green comments) I think what you actually should do is to change syntax coloring instead of developing an annotation.

In Preferences go to Java -> Editor -> Syntax Coloring , unfold Comments in the box on the right and choose whatever color you want (I suggest gray) for each type of the comments.

Additionally in Java -> Editor -> Folding make sure to have Comments selected.

Alternatively you could just remove all the comments. But it won't do unless you write self explanatory code. Start from the useless one. Refactor where they explain hard to comprehend code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM