Search code examples
checkstyle

Checking for a maximum level of indentation in Checkstyle


I want to ensure that my code does not exceed 5 levels of indentation, like so:

class Foo { // 0
    void bar() { // 1
        if () { // 2
            if () { // 3
                if () { // 4
                    if () { // 5
                        if () { // 6 - error
                        }
                    }
                }
            }
        }
    }
}

Looking through the Checkstyle documentation, I couldn't find a rule that specifically implements this. Does such a rule exist, or do I have to look into custom rules?


Solution

  • There is an issue still in "Open" state about this feature: https://github.com/checkstyle/checkstyle/issues/5487

    Not exactly what you need, but you can achieve something similar using the NestedForDepth, NestedIfDepth and NestedTryDepth checks.