Skip to content

max_statements

v1.0.0WarningConfigurableCode Quality

This rule flags a function that executes more statements than the configured budget.

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) {}

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 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.

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.

This rule is in the pedantic preset, so it is enabled by preset: pedantic or by name:

many_lints.yaml
rules:
max_statements:
enabled: true
analysis_options.yaml
many_lints:
rules:
max_statements:
max_statements: 25
Option Type Default Description
max_statements int 25 How many statements a function may execute

To disable this rule:

many_lints.yaml
rules:
max_statements: false

To keep the rule on but skip certain paths, use per-rule exclude.