Indentation

Since Checkstyle 3.1

Description

Checks correct indentation of Java code.

The idea behind this is that while pretty printers are sometimes convenient for bulk reformats of legacy code, they often either aren't configurable enough or just can't anticipate how format should be done. Sometimes this is personal preference, other times it is practical experience. In any case, this check should just ensure that a minimal set of indentation rules is followed.

Basic offset indentation is used for indentation inside code blocks. For any lines that span more than 1, line wrapping indentation is used for those lines after the first. Brace adjustment, case, and throws indentations are all used only if those specific identifiers start the line. If, for example, a brace is used in the middle of the line, its indentation will not take effect. All indentations have an accumulative/recursive effect when they are triggered. If during a line wrapping, another code block is found and it doesn't end on that same line, then the subsequent lines afterwards, in that new code block, are increased on top of the line wrap and any indentations above it.

Example:

if ((condition1 && condition2)
        || (condition3 && condition4)    // line wrap with bigger indentation
        ||!(condition5 && condition6)) { // line wrap with bigger indentation
  field.doSomething()                    // basic offset
      .doSomething()                     // line wrap
      .doSomething( c -> {               // line wrap
        return c.doSome();               // basic offset
      });
}
        

Properties

name description type default value since
arrayInitIndent Specify how far an array initialization should be indented when on next line. int 4 5.8
basicOffset Specify how far new indentation level should be indented when on the next line. int 4 3.1
braceAdjustment Specify how far a braces should be indented when on the next line. int 0 3.1
caseIndent Specify how far a case label should be indented when on next line. int 4 3.1
forceStrictCondition Force strict indent level in line wrapping case. If value is true, line wrap indent have to be same as lineWrappingIndentation parameter. If value is false, line wrap indent could be bigger on any value user would like. boolean false 6.3
lineWrappingIndentation Specify how far continuation line should be indented when line-wrapping is present. int 4 5.9
throwsIndent Specify how far a throws clause should be indented when on next line. int 4 5.7

Examples

To configure the default check:

<module name="Checker">
  <module name="TreeWalker">
    <module name="Indentation"/>
  </module>
</module>
        

Example of Compliant code for default configuration (in comment name of property that controls indentations):

class Test {
    String field;               // basicOffset
    int[] arr = {               // basicOffset
        5,                      // arrayInitIndent
        6 };                    // arrayInitIndent
    void bar() throws Exception // basicOffset
    {                           // braceAdjustment
        foo();                  // basicOffset
    }                           // braceAdjustment
    void foo() {                // basicOffset
        if ((cond1 && cond2)    // basicOffset
                  || (cond3 && cond4)    // lineWrappingIndentation, forceStrictCondition
                  ||!(cond5 && cond6)) { // lineWrappingIndentation, forceStrictCondition
            field.doSomething()          // basicOffset
                .doSomething()           // lineWrappingIndentation and forceStrictCondition
                .doSomething( c -> {     // lineWrappingIndentation and forceStrictCondition
                    return c.doSome();   // basicOffset
                });
        }
    }
    void fooCase()                // basicOffset
        throws Exception {        // throwsIndent
        switch (field) {          // basicOffset
            case "value" : bar(); // caseIndent
        }
    }
}
        

To configure the check to enforce the indentation style recommended by Oracle:

<module name="Checker">
  <module name="TreeWalker">
    <module name="Indentation">
      <property name="caseIndent" value="0"/>
    </module>
  </module>
</module>
        

Example of Compliant code for default configuration (in comment name of property that controls indentation):

void fooCase() {          // basicOffset
    switch (field) {      // basicOffset
    case "value" : bar(); // caseIndent
    }
}
        

To configure the Check to enforce strict condition in line-wrapping validation.

<module name="Checker">
  <module name="TreeWalker">
    <module name="Indentation">
      <property name="forceStrictCondition" value="true"/>
    </module>
  </module>
</module>
        

Such config doesn't allow next cases even code is aligned further to the right for better reading:

void foo(String aFooString,
        int aFooInt) { // indent:8 ; expected: 4; violation, because 8 != 4
    if (cond1
        || cond2) {
        field.doSomething()
            .doSomething();
    }
    if ((cond1 && cond2)
              || (cond3 && cond4)    // violation
              ||!(cond5 && cond6)) { // violation
        field.doSomething()
             .doSomething()          // violation
             .doSomething( c -> {    // violation
                 return c.doSome();
            });
    }
}
        

But if forceStrictCondition = false, this code is valid:

void foo(String aFooString,
        int aFooInt) { // indent:8 ; expected: > 4; ok, because 8 > 4
    if (cond1
        || cond2) {
        field.doSomething()
            .doSomething();
    }
    if ((cond1 && cond2)
              || (cond3 && cond4)
              ||!(cond5 && cond6)) {
        field.doSomething()
             .doSomething()
             .doSomething( c -> {
                 return c.doSome();
            });
    }
}
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.indentation

Parent Module

TreeWalker