NestedForDepth

Since Checkstyle 5.3

Description

Restricts nested for blocks to a specified depth.

Properties

name description type default value since
max Specify maximum allowed nesting depth. int 1 5.3

Examples

To configure the check:

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

Example:

for(int i=0; i<10; i++) {
    for(int j=0; j<i; j++) {
        for(int k=0; k<j; k++) { // violation, max allowed nested loop number is 1
        }
    }
}

for(int i=0; i<10; i++) {
    for(int j=0; j<i; j++) { // ok
    }
}
        

To configure the check to allow nesting depth 2:

<module name="Checker">
  <module name="TreeWalker">
    <module name="NestedForDepth">
      <property name="max" value="2"/>
    </module>
  </module>
</module>
        

Example:

for(int i=0; i<10; i++) {
    for(int j=0; j<i; j++) {
        for(int k=0; k<j; k++) {
            for(int l=0; l<k; l++) { // violation, max allowed nested loop number is 2
            }
        }
    }
}

for(int i=0; i<10; i++) {
    for(int j=0; j<i; j++) {
        for(int k=0; k<j; k++) { // ok
        }
    }
}
        

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.coding

Parent Module

TreeWalker