How to mark logical sections of code in Java comments?

前端 未结 11 2094
温柔的废话
温柔的废话 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: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.

提交回复
热议问题