Coding Standard

From NeoWiki

Jump to: navigation, search

Here is a description of the the coding standard being used in Neo and its sub components. A very basic rule is to just look at the current source and use common sense. We have exported an eclipse code formatter [[1]] that you can import (at least into eclipse). Once imported in eclipse you just hit Ctrl+Shift+F and it will format the code in the current window.

[edit] Class layout

Each file/class should begin with a comment including the copyright and license information followed by package definition then blank line followed by import statements.

/*
 * Copyright ....
 */
package org....

import org...
import org...

public class....

Wildcard imports are not allowed. Unused imports are frowned upon so if you use eclipse set the Unsued import warning to error (Window->Preferences->Java->Compiler->Errors/Warnings->Unnecessary Code).

[edit] Indentation and line length

Indentation is set to 4 spaces and the line length is 80. When breaking a line try to either break after a comma or after an operator. The new line should be indented.

// line break and indentation example
public class ...
{
    public Relationship createRelationship( Node startNode, Node endNode,
        RelationshipType type )
    {
        if ( startNode == null || endNode == null || type == null )
        {
            throw new IllegalArgumentException( "Null parameter, startNode=" +
                startNode + ", endNode=" + endNode + ", type=" + type );
        }
        RelationshipImpl rel = new RelationshipImpl( id, startNodeId,
            endNodeId, type, true, this );
...

[edit] Braces, white spaces, new lines and control statements

  • Opening brace always starts on a new line and is always followed by a new line unless it is an array initializer.
  • Closing brace is always followed by a new line (unless do-while then it is optional)
  • A comma is always followed by a white space (unless it is a parametrized type declaration).
  • Method declaration and invocation have a white space after opening and before closing parenthesis if parameters exist.
  • Operators have a whitespace before and after operator (except ++ and --).
  • Blank lines exist: after package declaration, before class declarations, after constructor or method declarations (unless next line i closing brace for class declaration).
  • Control statements (for,while,if...) always make use of opening and closing braces.
  • Parenthesized expressions and type casts does not need extra white space after opening or before closing parenthesis.
  • Array brackets do not have whitespace after opening or before closing bracket.
  • Parameterized types do not have whitespace before/after opening or before closing angle bracket, nor after/before the comma.
Personal tools