Skip to content

Deep nesting ​

Code longer than it needs to be: one of the ways AI coding agents bloat software, from lessloc's pattern catalog. lessloc catches it as deep-nesting.

What makes deep nesting a bad practice? ​

The standards that call it a bad practice: CWE-1124 Excessively Deep Nesting; Sonar S134; Fowler: Replace Nested Conditional with Guard Clauses.

What does lessloc do about deep nesting? ​

lessloc catches it as deep-nesting.

  • deep-nesting (tier 2): Statements nested 4 or more control levels deep.

Examples ​

Caught as deep-nesting:

python
def scan(rows):
    for r in rows:
        if r:
            for c in r:
                if c:
                    print(c)

Written well:

python
def scan(rows):
    for r in rows:
        for c in r or []:
            if c:
                print(c)

The other patterns in Code longer than it needs to be:

Released under the Apache-2.0 license. · llms.txt