Differences between revisions 1 and 2
Revision 1 as of 2008-02-20 09:31:46
Size: 3016
Comment:
Revision 2 as of 2010-03-17 13:09:35
Size: 3022
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
We try to follow the [http://java.sun.com/docs/codeconv/ Sun Code Conventions]. We try to follow the [[http://java.sun.com/docs/codeconv/|Sun Code Conventions]].
Line 51: Line 51:
You can download a configuration file for IntelliJ, containing the proper codestyle settings and code templates here: attachment:IJCodeStyleSettings.jar You can download a configuration file for IntelliJ, containing the proper codestyle settings and code templates here: [[attachment:IJCodeStyleSettings.jar]]

We try to follow the Sun Code Conventions.

Here are some highlights about what this means:

  • No more than 80 characters per line
  • Indent is 4 spaces
  • Break line BEFORE operators (if you break "x + y", "+" goes on the second line)
  • Use // comments for commenting out code or end-of-line comments, for multiline text comments

  • { goes on the same line as method/class/interface-declaration, } goes on a line by itself.
    • Exception 1: else/catch/finally each come on a line like "} else {"
    • Exception 2: in conjunction with log statements, such as if (log.isTraceEnabled()) log.trace(...);

  • no if/for/while/... without { ... }
  • spaces around operators, after commas, after if/while/... before {, and basically everywhere you can think of :)

  • package names are lowercase only. Class/interface names are capitalised. Method/variable names are NOT capitalised. Constants (static/final) are uppercase only. No _ except in constants
  • See the example: http://java.sun.com/docs/codeconv/html/CodeConventions.doc10.html

    log.trace("Entering storeRecord with record '"
              + rec.getId() + "'");
    /* doesValidate causes an external call to a
       XML-validator, so it is fairly slow. */
    if (doesValidate(rec)) {
        storage.store(rec);
        log.info("Stored validated record '"
                  + rec.getId() + "' to store '"
                  + store.getId() + "'");
    } else { // We have no alternative here so pass it on
        throw new StorageException("Could not store "
                                   + " record '"
                                   + rec.getId()
                                   + "' as it is invalid");
    }

If in doubt, refer to the documentation.

Furthermore the following should be observed:

  • No tabs in the code
  • TODO's are marked with

// TODO:
  • Bad implementations that should be fixed are marked with

// FIXME:

to get FIXME to work in Idea, go to Settings | IDE Settings | TODO and add the pattern \bfixme\b.* to the list.

  • Handle warnings from IDEA or the Java compiler

If IDEA warns about e.g. duplicate strings and it makes sense to have these duplicate strings, insert a suppress warning annotation or comment. This indicates a conscious choice and removes the warning. The square should be green, super green.

You can download a configuration file for IntelliJ, containing the proper codestyle settings and code templates here: IJCodeStyleSettings.jar

To use the configuration in your IDEA, go to the "File" menu, choose "Import settings" and select the JAR file just downloaded from this page. This will make IntelliJ enforce the above rules in most cases, however, please notice that it sometimes fail to correctly wrap lines (e.g. javadoc lines containing HTML tags) and that it has an annoying habit "fixing" (i.e. corrupting) properly formatted lines.

CodeStyle (last edited 2010-03-17 13:09:35 by localhost)