Since Checkstyle 5.3
Restricts nested for blocks to a specified depth.
| name | description | type | default value | since |
|---|---|---|---|---|
| max | Specify maximum allowed nesting depth. | int | 1 |
5.3 |
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
}
}
}
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.coding