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 Example1 {
    String field = "example";                  // basicOffset
    int[] values = {                           // basicOffset
        10,
        20,
        30
    };

    void processValues() throws Exception {
        handleValue("Test String", 42);          // basicOffset
    }

    void handleValue(String aFooString,
                     int aFooInt) {             // indent:8 ; expected: > 4;

        boolean cond1,cond2,cond3,cond4,cond5,cond6;
        cond1=cond2=cond3=cond4=cond5=cond6=false;

        if (cond1
            || cond2) {
            field = field.toUpperCase()
                .concat(" TASK");
        }

        if ((cond1 && cond2)
                || (cond3 && cond4)          // ok, lineWrappingIndentation
                || !(cond5 && cond6)) {      // ok, lineWrappingIndentation
            field.toUpperCase()
                 .concat(" TASK")             // ok, lineWrappingIndentation
                 .chars().forEach(c -> {      // ok, lineWrappingIndentation
                     System.out.println((char) c);
                 });
        }
    }

    void demonstrateSwitch() throws Exception {
        switch (field) {
            case "EXAMPLE": processValues();                        // caseIndent
            case "COMPLETED": handleValue("Completed Case", 456);   // 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):


class Example2 {
    String field = "example";                  // basicOffset
    int[] values = {                           // basicOffset
        10,
        20,
        30
    };

    void processValues() throws Exception {
        handleValue("Test String", 42);          // basicOffset
    }

    void handleValue(String aFooString,
                     int aFooInt) {

        boolean cond1,cond2,cond3,cond4,cond5,cond6;
        cond1=cond2=cond3=cond4=cond5=cond6=false;

        if (cond1
            || cond2) {
            field = field.toUpperCase()
                .concat(" TASK");
        }

        if ((cond1 && cond2)
                || (cond3 && cond4)           // ok, lineWrappingIndentation
                || !(cond5 && cond6)) {       // ok, lineWrappingIndentation
            field.toUpperCase()
                 .concat(" TASK")             // ok, lineWrappingIndentation
                 .chars().forEach(c -> {      // ok, lineWrappingIndentation
                     System.out.println((char) c);
                 });
        }
    }

    void demonstrateSwitch() throws Exception {
        switch (field) {
        case "EXAMPLE": processValues();                            // caseIndent
        case "COMPLETED": handleValue("Completed Case", 456);       // 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:


class Example3 {
    String field = "example";                  // basicOffset
    int[] values = {                           // basicOffset
        10,
        20,
        30
    };

    void processValues() throws Exception {
        handleValue("Test String", 42);
    }

    void handleValue(String aFooString,
                     int aFooInt) {      // violation, "incorrect indentation"

        boolean cond1,cond2,cond3,cond4,cond5,cond6;
        cond1=cond2=cond3=cond4=cond5=cond6=false;

        if (cond1
            || cond2) {
            field = field.toUpperCase()
                .concat(" TASK");
        }

        if ((cond1 && cond2)
                || (cond3 && cond4)        // violation, "incorrect indentation"
                || !(cond5 && cond6)) {    // violation, "incorrect indentation"
            field.toUpperCase()
                 .concat(" TASK")           // violation, "incorrect indentation"
                 .chars().forEach(c -> {    // violation, "incorrect indentation"
                     System.out.println((char) c);
                 });
        }
    }

    void demonstrateSwitch() throws Exception {
        switch (field) {
            case "EXAMPLE": processValues();                            // caseIndent
            case "COMPLETED": handleValue("Completed Case", 456);       // caseIndent
        }
    }
}
        

But if forceStrictCondition = false, this code is valid:


class Example4 {
    String field = "example";                   // basicOffset
    int[] values = {                            // basicOffset
        10,
        20,
        30
    };

    void processValues() throws Exception {
        handleValue("Test String", 42);
    }

    void handleValue(String aFooString,
                     int aFooInt) {            // indent:8 ; expected: > 4

        boolean cond1,cond2,cond3,cond4,cond5,cond6;
        cond1=cond2=cond3=cond4=cond5=cond6=false;

        if (cond1
            || cond2) {
            field = field.toUpperCase()
                .concat(" TASK");
        }

        if ((cond1 && cond2)
                || (cond3 && cond4)          // ok, lineWrappingIndentation
                || !(cond5 && cond6)) {      // ok, lineWrappingIndentation
            field.toUpperCase()
                 .concat(" TASK")            // ok, lineWrappingIndentation
                 .chars().forEach(c -> {     // ok, lineWrappingIndentation
                     System.out.println((char) c);
                 });
        }
    }

    void demonstrateSwitch() throws Exception {
        switch (field) {
            case "EXAMPLE": processValues();                            // caseIndent
            case "COMPLETED": handleValue("Completed Case", 456);       // caseIndent
        }
    }
}
        

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