max_statements
This rule flags a function that executes more statements than the configured budget.
Why use this rule
Section titled “Why use this rule”This is the counterpart to avoid_long_functions, measuring what a function does rather than how much room it takes. The two disagree in exactly the useful cases: a function of twenty short statements is long by both measures, while one holding a single wide widget tree is long by lines only, and splitting it would help nobody.
This rule is in the pedantic preset: a budget is a house style, and the right number differs per codebase.
With max_statements: 5:
void handleSubmit(String email, String password) { final trimmedEmail = email.trim(); final trimmedPassword = password.trim(); final emailValid = trimmedEmail.contains('@'); final passwordValid = trimmedPassword.length >= 8; final ready = emailValid && passwordValid; print(ready); // LINT: 6 statements, over the limit of 5}Give each step a name and the caller reads as a summary:
void handleSubmit(String email, String password) { final credentials = _validate(email, password); if (credentials == null) return; _submit(credentials);}
({String email, String password})? _validate(String email, String password) { final trimmedEmail = email.trim(); if (!trimmedEmail.contains('@')) return null; if (password.length < 8) return null; return (email: trimmedEmail, password: password);}
void _submit(({String email, String password}) credentials) {}Nesting does not hide statements
Section titled “Nesting does not hide statements”Statements are counted through blocks, so a loop body counts toward the function that owns it. The for, the if and the continue each count as one on top of what they contain — this is 6:
void report(List<String> lines) { var total = 0; // 1 for (final line in lines) { // 2 if (line.isEmpty) { // 3 continue; // 4 } total += line.length; // 5 } print(total); // 6}A callback is measured on its own
Section titled “A callback is measured on its own”A nested function expression is separate work, not more of the enclosing function’s, so it is not counted into the caller:
void install(List<String> names) { names.forEach((name) { final trimmed = name.trim(); print(trimmed); print(trimmed.length); });}install counts 1 statement — the forEach call.
Known limitations
Section titled “Known limitations”Only block bodies are counted. An expression body (=>) has one expression and is never reported, however wide it is.
Constructors are checked too, alongside functions and methods; a constructor with a long body is reported at its name.
Enabling this rule
Section titled “Enabling this rule”This rule is in the pedantic preset, so it is enabled by preset: pedantic or by name:
rules: max_statements: enabled: trueOptions
Section titled “Options”many_lints: rules: max_statements: max_statements: 25rules: max_statements: max_statements: 25| Option | Type | Default | Description |
|---|---|---|---|
max_statements |
int | 25 |
How many statements a function may execute |
Turning this rule off
Section titled “Turning this rule off”To disable this rule:
rules: max_statements: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”avoid_complex_conditions— Keep boolean conditions within an operand budget.avoid_deep_nesting— Keep control flow within a nesting budget.avoid_high_cyclomatic_complexity— Keep a function within a complexity budget.avoid_long_functions— Keep function bodies within a line budget.