Skip to content

Coding Conventions

Alexander Alexandrov edited this page Feb 4, 2014 · 1 revision

The Aura code style is based on the Java style from the google-styleguide project.

Amendments

In order to improve readability, we introduce the following set of amendments on top of google-styleguide.

1. Line Width

The maximum line with is increased to 150 characters. This fits well in modern widescreens even if the sidepanel of your IDE is visible.

2. Indent Size

The default indent is set to 4 spaces. This increases readability in of branched code typical for system programming.

3. Parameter Lists

Parameter lists on constructor and method declarations are aligned to the opening brace if they don't fit on the line.

Example:

public class Foo {

    public void bar(String someArgument, 
                    Integer anotherArgument, 
                    Float yetAnotherArgument) {
        ...
    }
}

4. Throws, Extends and Implements Clauses

Type lists after throws, extends and implements clauses are wrapped if necessary (all but the first element), and indented by 8 spaces.

Example:

public class Foo extends SomeOtherClass implements 
        InterfaceOne, 
        InterfaceTwo {

    public void bar(String someArgument) throws 
            IllegalArgumentException,
            IndexOutOfBoundsException,
            IOException,
            FileNotFoundException {
        ...
    }
}

5. Enums

Enum constants are always indented by 4 spaces.

Example:

public class Foo {

    public Enum Bar {
        SOME_VALUE(1, 2),
        SOME_THER_VALUE(3, 4),
        YET_ANOTHER_VALUE(5, 6)
    }
}

6. Arguments in Method and Constructor Calls

Arguments in method and constructor invocations are wrapped if necessary and indented on the column of the first argument.

Example:

public class Foo {
    
    public void Foo() {
        super("Some Value", 
              "Some Other Value",
              "Yet Another Value");
    }

    public String bar(Some a, String someArgument, someOtherArgument) {
        return a.someMethod(someArgument, 
                            someOtherArgument);
    }
}

7. Chained Method Invocations

Chained method invocations, typical for fluent APIs are wrapped if necessary and indented on the column of the first method call.

Example:

public class Foo {
    
    public String bar() {
        return a.someMethod()
                .someOtherMethod()
                .yetAnotherMethod();
    }
}

8. Declaration Order in Classes

Classes must implement the following section of five basic sections. Each section starts with a block comment. Empty sections can be omitted. Declaration of elements inside a section should be ordered on modifier: public, protected, private.

public class Baz {

    // ---------------------------------------------------
    // Constants.
    // ---------------------------------------------------

    ...

    // ---------------------------------------------------
    // Fields.
    // ---------------------------------------------------

    ...

    // ---------------------------------------------------
    // Constructors.
    // ---------------------------------------------------

    ...

    // ---------------------------------------------------
    // Methods.
    // ---------------------------------------------------

    ...

    // ---------------------------------------------------
    // Inner Classes.
    // ---------------------------------------------------

    ...
}

9. Conditional Formatting

A code block between the special comments @formatter:off and @formatter:on is not formatted.

Example:

public class Foo {
    
    public String bar() {
        // @formatter:off
        return custom
                      .formatted()
                                   .code();
        // @formatter:on
    }
}

Maven Java Formatter

An code style profile for Еclipse following the above guidelines can be found in the aura-build-tools package.

The format is configured with the maven-java-formatter-plugin to be applied automatically on each maven build. You can also apply the format manually from the console using the dedicated Maven goal:

mvn java-formatter:format

IDE Integration

In order to use the Aura code format from your IDE, follow the instructions below.

Eclipse Users

  1. Go to Eclise | Windows | Preferences | Java | Code Style | Formatter.
  2. Import the formatter.xml profile and configure it for the Aura project.

Tested with Eclipse 4.2 (Juno).

IntelliJ Users

  1. Install the Eclipse Code Formatter plugin.
  2. Go to Settings | Eclipse Code Formatter and poin to the formatter.xml in the Eclipse Java Formatter config file field.

Tested with IntelliJ 13.0.2.