Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.


  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. Avoid static members as much as possible. Such use often make it difficult later when the class needs to be used in wider context.
  6. Where possible, use final members. It can help improve the readability significantly.
  7. 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

Consider using attached coding style files for IntelliJ and Eclipse.

Here is a sample code to demonstrate above guidelines: