Why does EK9 reject a single deeply-nested coalescing expression?

← Code Quality · Ref: Q1302

EK9 measures expression complexity independently of method complexity. Nested coalescing operators (?:, ??, <?, >?, <=?, >=?) carry an exponential 3^(depth-1) penalty, so three nested levels reach 17 points and exceed the 15-point threshold, raising E11013. The fix is to extract named intermediate variables (step1<-..., step2<-...), which flattens the depth to 1 each while producing an identical result.

See Q311 for quality checks.

Example

defines module qa.quality.expression.complexity

  defines function

    //THE FIX: extract intermediate named variables instead of one nested expression.
    //Each coalescing operator is now at depth 1, so total cost is 1 + 2 + 2 = 5 (well under 15).
    computeFallback() as pure
      ->
        a as Integer
        b as Integer
        c as Integer
        d as Integer
      <-
        result as Integer?
      step1 <- a ?: b
      step2 <- step1 <? c
      result: step2 >? d

  defines program

    ExpressionComplexityDemo()
      stdout <- Stdout()
      result <- computeFallback(a: Integer(), b: 2, c: 3, d: 4)
      stdout.println(`Result is ${result}`)

Common mistakes

E11013 — The single nested expression costs 1*9 + 2*3 + 2*1 = 17 points (3^(depth-1) per level), exceeding the 15-point expression threshold. Extracting each coalescing step into its own named variable drops every expression to depth 1 (1 + 2 + 2 = 5 points total), so none exceeds the threshold while the computed result is identical.

Incorrect:

result <- ((a ?: b) <? c) >? d

Correct:

      step1 <- a ?: b
      step2 <- step1 <? c
      result: step2 >? d
Other ways to ask this
  • What triggers E11013 EXCESSIVE_EXPRESSION_COMPLEXITY?
  • How do I fix an expression that is too complex in EK9?
  • Why must I extract intermediate variables from nested coalescing?

Coming from another language?

Java: no expression-level complexity analysis; nested ternaries compile silently. Kotlin/Swift: chained ?:/?? unlimited, no linter rule for coalescing depth. EK9: compile-time error forcing extraction into readable intermediate variables.

Keywords: coalescing, extract, quality, complexity, E11013, variable, intermediate, expression