How many operators can one EK9 expression have, and how deeply can code nest?
← Code Quality · Ref: Q1393
EK9 bounds the shape of source as it is read, before it is parsed: one expression may hold at most 20 operators and at most 20 member accesses ('.'), both reported as E11013, and brackets ('(', '[' and '{' together) may nest at most 10 deep and blocks at most 20, both E11011. An expression ends at a comma or at the end of its statement; a line ending in a binary operator carries it on. Real code stays well inside: at most 15 operators and 7 member accesses in an expression. Coalescing chains are held tighter still by the expression-complexity score: at most 3 of ?? and ?:, or 2 of <? >? <=? >=?.
The fix is always to name a part: extract an intermediate variable (or a function) and use the name. Each limit is reported once, before the parser runs, and nothing else in that file is reported until it is fixed.
See Q1302 for coalescing complexity and Q814 for control-flow nesting.
Example
defines module qa.quality.expression.limits defines constant isbnLength <- 10 defines function //THE FIX: as one expression this check would hold 23 operators - past the limit of 20 (E11013). //Naming the weighted sum keeps each expression short, and says what the arithmetic is for. isbnTenIsValid() as pure -> digits as List of Integer <- valid as Boolean? weightedSum <- digits.getOrDefault(0, 0) * 10 + digits.getOrDefault(1, 0) * 9 + digits.getOrDefault(2, 0) * 8 + digits.getOrDefault(3, 0) * 7 + digits.getOrDefault(4, 0) * 6 + digits.getOrDefault(5, 0) * 5 + digits.getOrDefault(6, 0) * 4 + digits.getOrDefault(7, 0) * 3 + digits.getOrDefault(8, 0) * 2 + digits.getOrDefault(9, 0) valid: length digits == isbnLength and weightedSum mod 11 == 0 //THE FIX: as one expression these brackets would nest 11 deep - past the limit of 10 (E11011). //Naming the inner part splits the nesting across two expressions. scaled() as pure -> amount as Integer <- result as Integer? inner <- ((((amount + 1) * 2) + 3) * 4) result: ((((((inner + 5) * 6) + 7) * 8) + 9) * 10) defines program ExpressionLimitsDemo() stdout <- Stdout() //0-306-40615-2 digits <- [0, 3, 0, 6, 4, 0, 6, 1, 5, 2] stdout.println(`Valid: ${isbnTenIsValid(digits)}`) stdout.println(`Scaled: ${scaled(1)}`)
Common mistakes
E11013 — As one expression the check holds 23 operators: length, ==, and, nine *, nine +, mod and ==. One expression may hold at most 20, so it is rejected as it is read. Naming the weighted sum leaves 18 operators in one expression and 5 in the other, and the result is identical.
Incorrect:
valid: length digits == isbnLength and (digits.getOrDefault(0, 0) * 10 + digits.getOrDefault(1, 0) * 9 + digits.getOrDefault(2, 0) * 8 + digits.getOrDefault(3, 0) * 7 + digits.getOrDefault(4, 0) * 6 + digits.getOrDefault(5, 0) * 5 + digits.getOrDefault(6, 0) * 4 + digits.getOrDefault(7, 0) * 3 + digits.getOrDefault(8, 0) * 2 + digits.getOrDefault(9, 0)) mod 11 == 0
Correct:
weightedSum <- digits.getOrDefault(0, 0) * 10 + digits.getOrDefault(1, 0) * 9 + digits.getOrDefault(2, 0) * 8 + digits.getOrDefault(3, 0) * 7 + digits.getOrDefault(4, 0) * 6 + digits.getOrDefault(5, 0) * 5 + digits.getOrDefault(6, 0) * 4 + digits.getOrDefault(7, 0) * 3 + digits.getOrDefault(8, 0) * 2 + digits.getOrDefault(9, 0) valid: length digits == isbnLength and weightedSum mod 11 == 0
E11011 — Brackets nest 11 deep; the limit is 10, counting '(', '[' and '{' together. Naming the inner part splits the nesting across two expressions, neither deeper than 6.
Incorrect:
result <- (((((((((((amount + 1) * 2) + 3) * 4) + 5) * 6) + 7) * 8) + 9) * 10) + 11)
Correct:
inner <- ((((amount + 1) * 2) + 3) * 4) result: ((((((inner + 5) * 6) + 7) * 8) + 9) * 10)
Other ways to ask this
- What does '21 operators in one expression - the limit is 20' mean?
- Why is my long formula rejected with E11013?
- What are EK9's hard limits on expressions, brackets and blocks?
- How many chained method calls can one expression have?
Coming from another language?
Java: javac accepts any expression length; very long expressions eventually fail as 'code too large' or a StackOverflowError in the compiler. C: the standard only guarantees 63 nesting levels. Python: the parser limits nesting to 200. SonarQube S1067 flags more than 3 boolean operators, as advice. EK9: a hard limit at 20 operators, checked before parsing.
Keywords: nesting, limit, E11011, chain, E11013, quality, operators, expression, intermediate, brackets