You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Current »

Suggested coding guidelines

  1. Indentation: suggest to use 4 spaces; 2 spaces works as well – but be consistent! No tabs please!
  2. Spaces:
    1. a single space before “(“, “{“
    2. a single space after “)”
    3. a single space around operators
    4. Example: “if (a == b && x != y) {“; avoid “if(a==b&&x!=y){“
  3. Blocks: “{“, at the end of the line that starts the block, end the block with “}” in a separate line
  4. Statements: earlier restrictions on line length (of 80/132) were likely for printing of the code on paper. Most of us shouldn’t be printing source code on paper now. So, a statement need not be broken into multiple lines – even if it is few hundred characters long.
  5. Class member declaration in the following order:
    1. static members: public, protected, private; within each accessor, ‘final’ members precede non-final
    2. instance members: public, protected, private; within each accessor, ‘final’ members precede non-final
    3. Vertically align start of: typeName, variableName, assignment operator
    4. methods: public, protected, private; within each accessor, static methods precede non-static
    5. methods: all constructors immediately after ‘public static’ methods
    6. methods: all getters/setters immediately after constructors
    7. methods: all @Override methods immediately after getter/setters
    8. methods: use a consistent name for variable holding the return value. Suggested name ‘ret’
    9. methods: use a single return for each method (as much as possible)
    10. methods: avoid methods with large number of lines; suggested length 24 (from good old days!)
    11. Blank line before and after each ‘for/while/do/if-else’ block
    12. Blank line after each variable declaration block
    13. Separate assignment lines from the rest

Here is a sample code to demonstrate above guidelines:

class MyClass {
    public static final String CONFIG_INDEX_TYPE           = "index.type";
    public static final String CONFIG_INDEX_TYPE_DEF_VALUE = "solr";

    private static final Logger LOG = LoggerFactory.getLogger(MyClass.class);

    protected final String id;
    protected       String updatedBy;

    private final String name;
    private       int    count;


    public static void main(String[] args) {
        // ...
    }

    public MyClass(String name) {
this.id = 0;
        this.name = name;

        init();
    }

    public String getId() { return id; }

    public String getName() { return name; }

    public String getUpdatedBy() { return updatedBy; }

    public void setUpdatedBy(String updatedBy) { this.updatedBy = updatedBy; }

    public boolean doWork() {
        boolean ret = true;

        for (int i = 0; i < count; i++) {
            if (count > 0 && count % 1000) {
                LOG.info(...);
            }

            test();
        }

        return ret;
    }

    private void init() {
        this.count = 0;

        test();

        // ...
    }
}



  • No labels