Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mark logical sections of code in Java comments?

Tags:

java

comments

Java classes are generally divided into logical "blocks". Is there a convention to mark these sections? Ideally, it would be supported by the major IDEs.

I personally use this method:

//// Section name here //// 

However, some editors seem to have problems with this.

As an example, in Objective-C code you can use this method:

#pragma mark - #pragma mark Section name here 

This will result in a menu in XCode that looks like this:

alt text

like image 518
Frederik Avatar asked Mar 04 '10 12:03

Frederik


People also ask

How do you comment sections in Java?

Single-line comments start with two forward slashes ( // ). Any text between // and the end of the line is ignored by Java (will not be executed).

How do you comment multiple lines in Java?

Multi line comments in Java start with /* and end with */. You can comment multiple lines just by placing them between /* and */.

What are the three techniques for adding comments to a Java program?

Single line, multi-line and documentation are the three ways to present the comments in Java.

How can you best outlined the importance of commenting in Java programming?

Comments in Java are the statements that are not executed by the compiler and interpreter. It can be used to provide information or explanation about the variable, method, class or any statement. It can also be used to hide program code for a specific time.


2 Answers

For intellij/android studio there is an amazing solution.
Start with:
//region Description
and end with:
//endregion

The shortcut for that is in the menu you can open with Command+Alt+T (Mac) or Ctrl+Alt+T (Windows)

You can also add your own line for additional visual separation if you need it. The region can be contracted and expanded at will with the +/- buttons like any function. You can also navigate between regions with Command+Alt+Period (Ctrl+Alt+Period)

Source.

Example:

//region Parceler Implementation //--------------------------------------------------------------------------------------- @Override public int describeContents() {     return 0; }  @Override public void writeToParcel(Parcel dest, int flags) {     dest.writeParcelable(this.die, 0);     dest.writeParcelable(this.dieSprite, 0); }  private DieVm(Parcel in) {     this.die = in.readParcelable(Die.class.getClassLoader());     this.dieSprite = in.readParcelable(Sprite.class.getClassLoader()); }  public static final Parcelable.Creator<DieVm> CREATOR = new Parcelable.Creator<DieVm>() {     public DieVm createFromParcel(Parcel source) {         return new DieVm(source);     }      public DieVm[] newArray(int size) {         return new DieVm[size];     } }; //--------------------------------------------------------------------------------------- //endregion 
like image 69
Andrey Petrov Avatar answered Sep 22 '22 12:09

Andrey Petrov


I personally use 80-chars line separators, like this :

public class Client {      //================================================================================     // Properties     //================================================================================      private String name;     private boolean checked;      //================================================================================     // Constructors     //================================================================================      public Client() {     }      public Client(String name, boolean checked) {         this.name = name;         this.checked = checked;     }      //================================================================================     // Accessors     //================================================================================      public String getName() {         return name;     }      public void setName(String name) {         this.name = name;     }      public boolean isChecked() {         return checked;     }      public void setChecked(boolean checked) {         this.checked = checked;     }  } 

Of course, this may seem a bit overkill for such a small POJO, but believe me, it proved very useful in some huge projects where I had to browse through big source files and quickly find the methods I was interested in. It also helps understand the source code structure.

In Eclipse, I have created a set of custom templates (Java -> Editor -> Templates in Eclipse's Preferences dialog) that generate those bars, eg. - sepa (SEParator for Accessors) - sepp (SEParator for Properties) - sepc (SEParator for Constructors) - etc.

I also modified the standard "new class" template (Java -> Code Style -> Code Templates in Eclipse Preferences screen)

Also, there is an old Eclipse plugin called Coffee-bytes, which enhanced the way Eclipse folds portions of code. I don't know if it still works, but I remembed one could define arbitrary foldable zones by adding special comments, like // [SECTION] or something. It might still work in recent Eclipse revisions, so take a look.

like image 45
Olivier Croisier Avatar answered Sep 21 '22 12:09

Olivier Croisier