Project

General

Profile

Actions

Java Code Formatting Conventions

This page describes the best practices when writing or reviewing Java code


General

In general it is recommended to follow SUNs Java Code Conventions TODO update link

In particular this means:

  • no tabs, use only spaces! TODO is this still true?

  • no commented code without reason why it is commented, but in general try to avoid commented code

  • add comments/javadoc as much as possible

Classes

  • start interfaces with capital I (e.g. ICdmBase )
  • add suffix Base to abstract classes (e.g. CdmBase)

Header

  • no $ID statement at beginning
  • always add licence statement
  • no empty line between licence statement and package line
  • add javadoc on class
    • with minimum @author and @since
    • no empty lines at start or end
    • empty line between description text and tags

Body

  • line break in class statement before extends and impelements (not necessary for short lines)
  • never more then 1 empty line (with very few exceptions if there is an important reason)
  • no empty lines before end-of-code-block brackets
  • remove unnecessary information from javadoc, e.g. parameters or return statements without description; fully remove javadoc that has no description and auto-generated javadoc for getters and setters
  • add 1 empty line between methods (optional if methods belong together as getter/setter/adder/remover/putter)
  • for logging use java private static final Logger logger = LogManager.getLogger();
  • add static methods at the beginning

Updated by Andreas Müller about 2 months ago · 10 revisions