Skip to content
Ryan Moore edited this page Aug 11, 2016 · 7 revisions

Spectra Java Style Guide

When contributing to open source spectra java projects we request that the code conforms to the following style guide. This speeds up code reviews, and make sure that we have a consistent style across all our open source software projects, regardless of who is contributing.

Indentation

Code indentation must use 4 spaces. If indenting twice, use 8 spaces.

Example:

public void func() {
    if (true) {
        System.out.println("message");
    }
}

Newline

The \n character should be used for all newlines.

Use of final Keyword

All variables should use the final keyword if it is not being reassigned.

Example:

public void func() {
    // var is not reasigned, and is therefore final
    final Object var = new Object();
    
    // var2 is reassigned, and is therefore not final
    Object var2 = new Object();
    var2 = new Object();
}

Method parameters should also have the final keyword prepended to them. Example:

public void func(final Object parameter1, final Object parameter2) {
    System.out.println("Message");
}

Another place where final should be used is in exception handling.

Example:

try {
    aMethodCallThatThrowsAnException();
} catch(final Exception e) {
    LOG.error("Some message about the exception", e);
    handleExceptionSomehow();
}

final Classes

If a class has all static methods, like a utils class, it must be declared final

Example:

public final class Util {
    public static void doSomething() {
        System.out.println("static util method, in a final class");
    }
}

{ Placement

The { should be placed at the end of the line with one space to the left, followed by a newline.

Example:

public void func() {
    if (true) {
        System.out.println("Message");
    }
}

Conditional Statements

Following a conditional statement i.e. (if, while, for) there must be one space followed by the conditional statement. Note: Curly braces should never be omitted, when where it would still be syntactically correct.

Example:

public void func() {
    if (true) {
        System.out.println("Message")
        for (int i = 0; i < 10; i++) {
            System.out.println("Print " + i);
        }
    }
}

Constants

Constant variables should be declared to use the final and static keywords, and it should be declared in all caps. If the variable would be camel cased use _ instead.

Example:

public class Example {
    private static final Logger LOG = LoggerFactory.getLogger(Example.class); // Always name the logger with the class name
    private static final String CONSTANT_VARIABLE = "constant variable";
    
    public void method() {
        LOG.info("Log something");
    }
}

LOG statements

In the SDK and CLI, LOG statements are useful to users and developers. There can be a performance hit if the LOG statement is not used correctly, in which messages are not printed at the current LOG_LEVEL are still evaluated.

The following two lines will yield the exact same output. However, in case of a disabled logging statement, the second variant will outperform the first variant by a factor of at least 30.

logger.debug("The new entry is " + entry + "."); // Incorrect
logger.debug("The new entry is {}.", entry); // Correct
Clone this wiki locally