E11012: too many statements - extract functions
What went wrong
Your function/method has too many statements - it's doing too much. Long functions are harder to understand, test, and maintain.
Worked examples
Compilable examples demonstrating this error and the correct fix:
- ek9 -q 1335 (Why does EK9 reject a method or function that has too...)
Thresholds
- Operators: 50 statements
- Methods: 100 statements
- Functions: 150 statements
- Service operations: 100 statements
What counts as a statement
- Variable declarations (x <- 5)
- Assignments (x := y + z)
- Control flow entries (if, while, for, switch, try)
- Throw statements
- Call statements: a function call, a method call (stdout.println(x)), x++ / x--, a stream statement
- require, assert, assertThrows, assertDoesNotThrow and assertPanic statements
Example correct refactoring
Before: processOrder() with 120 statements
After: processOrder() calls:
validateOrder() // ~20 statements calculateTotals() // ~25 statements persistOrder() // ~30 statements sendNotification() // ~15 statements
How to fix it
- Identify logical groups that form a sub-task
- Extract each group into a well-named helper function
- Each function should do ONE thing well (Single Responsibility)
Common causes
- Too many responsibilities in one function
- Missing helper functions
See also
https://ek9.io/quality.html#complexity https://ek9.io/forAI.json#$.qualityMetrics.metrics[?(@.errorCode=="E11012")]
Examples
Every example is compiled by the EK9 compiler: the first fails exactly as captioned, the others fix it.
Fails with E11012: too many statements - extract functions
#!ek9 defines module error.e11012.bad defines class TestClass mainValue as Integer: 0 default operator ? //This operator has 56 statements, exceeding the max of 50 for operators //Note: Can't use 'pure' because we need to do mutation inside operator :=: -> other as TestClass v1 <- 1 v2 <- v1 + 1 v3 <- v2 + 1 v4 <- v3 + 1 v5 <- v4 + 1 v6 <- v5 + 1 v7 <- v6 + 1 v8 <- v7 + 1 v9 <- v8 + 1 v10 <- v9 + 1 v11 <- v10 + 1 v12 <- v11 + 1 v13 <- v12 + 1 v14 <- v13 + 1 v15 <- v14 + 1 v16 <- v15 + 1 v17 <- v16 + 1 v18 <- v17 + 1 v19 <- v18 + 1 v20 <- v19 + 1 v21 <- v20 + 1 v22 <- v21 + 1 v23 <- v22 + 1 v24 <- v23 + 1 v25 <- v24 + 1 v26 <- v25 + 1 v27 <- v26 + 1 v28 <- v27 + 1 v29 <- v28 + 1 v30 <- v29 + 1 v31 <- v30 + 1 v32 <- v31 + 1 v33 <- v32 + 1 v34 <- v33 + 1 v35 <- v34 + 1 v36 <- v35 + 1 v37 <- v36 + 1 v38 <- v37 + 1 v39 <- v38 + 1 v40 <- v39 + 1 v41 <- v40 + 1 v42 <- v41 + 1 v43 <- v42 + 1 v44 <- v43 + 1 v45 <- v44 + 1 v46 <- v45 + 1 v47 <- v46 + 1 v48 <- v47 + 1 v49 <- v48 + 1 v50 <- v49 + 1 v51 <- v50 + 1 v52 <- v51 + 1 v53 <- v52 + 1 v54 <- v53 + 1 //Sum and store to ensure referenced this.mainValue := v54
Fixed: Keep an operator short - extract the work into a function
#!ek9 defines module error.e11012.good defines class TestClass mainValue as Integer: 0 default operator ? operator :=: -> other as TestClass this.mainValue := chained(other.mainValue) defines function chained() -> start as Integer <- rtn as Integer: start + 53
Questions and answers
These Q&A entries show E11012 and its fix:
Found a problem?
If EK9 reported E11012 for a program you think is valid, or did not report it where it should have, use the compiler form with the smallest program that shows it. If this page is wrong or unclear, use the documentation form. Both are at github.com/ek9lang/ek9-issues.