E11011: excessive nesting - extract function
What went wrong
EK9 limits control flow nesting to 6 levels as an absolute structural backstop. Each level of nesting is one more condition a reader must hold in mind.
There is also a hard limit on the SOURCE itself: brackets '(', '[' and '{' (counted together) may nest at most 10 deep, and blocks (indentation levels) at most 20. A source beyond either is reported here - "nested 11 deep - the limit is 10 brackets" - when it is read, before it is parsed, and nothing else in that file is reported until it is fixed. Name the inner parts: extract intermediate variables or functions.
Worked examples
Compilable examples demonstrating this error and the correct fix:
- ek9 -q 814 (How does EK9 enforce nesting depth limits?)
[RELATIONSHIP WITH E11021 (Cognitive Complexity)] E11011 counts nesting levels (simple structural limit). E11021 accounts for declaration guards (<-) cognitive load. Code with guards may trigger E11021 at depth 4-5.
How to fix it
- Extract nested logic into helper functions
- Use early returns via guards
- Replace nested ifs with switch when appropriate
- Use stream pipelines for data transformations
Common causes
- Too many nested if/while/for/switch/try blocks
- Deep callback or handler chains
- Complex conditional logic not extracted to helpers
See also
https://ek9.io/quality.html#complexity https://ek9.io/forAI.json#$.qualityMetrics.metrics[?(@.errorCode=="E11011")]
Examples
Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.
Fails with E11011: excessive nesting - extract function
#!ek9 defines module error.e11011.bad defines constant LEVEL_TWO <- 2 LEVEL_THREE <- 3 LEVEL_FOUR <- 4 LEVEL_FIVE <- 5 LEVEL_SIX <- 6 defines function deepSimpleNesting() -> mainValue as Integer stdout <- Stdout() if mainValue > 0 if mainValue > 1 if mainValue > LEVEL_TWO if mainValue > LEVEL_THREE if mainValue > LEVEL_FOUR if mainValue > LEVEL_FIVE if mainValue > LEVEL_SIX stdout.println(`${mainValue} is above every level`)
Fixed: Extract the inner levels into a function of their own
#!ek9 defines module error.e11011.good defines constant LOW <- 3 HIGH <- 6 defines function innerLevel() as pure -> mainValue as Integer <- rtn as Integer: 0 if mainValue > HIGH rtn: 7 else if mainValue > LOW rtn: 4 shallowNesting() -> mainValue as Integer <- result as Integer: 0 if mainValue > 0 result: innerLevel(mainValue)
Questions and answers
These Q&A entries show E11011 and its fix: