How to mark logical sections of code in Java comments?

前端 未结 11 2076
温柔的废话
温柔的废话 2021-01-29 19:42

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

相关标签:
11条回答
  • 2021-01-29 20:11

    As far as I know there is no such thing as a supported specification for grouping class members together. You can use what-ever comment convention you like, but chances are it will not be supported by any tool.

    It is better to group related members into separate class via inheritance or aggregation. This is considered a good OOP style

    0 讨论(0)
  • 2021-01-29 20:12

    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.

    0 讨论(0)
  • 2021-01-29 20:13

    For IntelliJ i do like:

            public void ________________INIT__________________() {};
    

    looking pretty in file structure!

    0 讨论(0)
  • 2021-01-29 20:18

    I liked that also when i was using xcode. For eclipse i use ctrl+o (quick outline) to navigate through a Java class.

    0 讨论(0)
  • 2021-01-29 20:18

    If you can cluster your methods, do another class specifically for that concept that you want to capture in a section. Go ahead, creating files is free.

    0 讨论(0)
  • 2021-01-29 20:22

    I would use javadoc; or use the following as a simple "separator" (single or 3 lines):

    /** RecyclerOnItemClickListener */
    
    /** 
     * RecyclerOnItemClickListener
     */
    

    So that in IDE it appears in a different color other than the unobtrusive commented grey.

    0 讨论(0)
提交回复
热议问题