Skip to content

max_function_length

Reports a function that has more lines of code than the limit. Move its steps into functions with a name.

Tier
advisory
Scope
Dart
Fix
none

A function with more lines of code than the limit max. A function exactly at max is not reported. The violation is on the function’s name: “This function has 72 lines of code, which is over the limit of 60.”

  • The length is the number of lines of the body that carry code, from its opening brace to its closing brace, both included.
  • Blank lines and lines that hold only a comment do not count.
  • The documentation comment, annotations, signature and a constructor’s initializers do not count.
  • Each function is measured on its own, closures and local functions included. A closure’s lines count for the function around it too.

A long function does several things, and you have to keep all of them in mind to change one; no part of it can be tested alone. An AI coding agent adds the next step where it is, because moving it into a function of its own is a second decision nothing asks for, and a limit asks. The rule is advisory because length alone proves nothing: a function that builds one widget tree or fills one table can be long and plain.

Violatingwith max: 12
Future<Receipt> checkout(Cart cart, Customer customer) async {
if (cart.items.isEmpty) {
throw StateError('The cart is empty.');
}
var net = 0;
for (final item in cart.items) {
net += item.price * item.quantity;
}
final discount = customer.isMember ? net ~/ 10 : 0;
final tax = (net - discount) * taxPercent ~/ 100;
final total = net - discount + tax;
final payment = await gateway.charge(customer.card, total);
if (!payment.accepted) {
throw PaymentRefused(payment.reason);
}
await orders.save(Order(cart.items, total, payment.id));
return Receipt(total: total, tax: tax, paymentId: payment.id);
}
Corrected
Future<Receipt> checkout(Cart cart, Customer customer) async {
if (cart.items.isEmpty) {
throw StateError('The cart is empty.');
}
final price = priceOf(cart, customer);
final payment = await charge(customer, price.total);
await orders.save(Order(cart.items, price.total, payment.id));
return Receipt(total: price.total, tax: price.tax, paymentId: payment.id);
}
Price priceOf(Cart cart, Customer customer) {
var net = 0;
for (final item in cart.items) {
net += item.price * item.quantity;
}
final discount = customer.isMember ? net ~/ 10 : 0;
final tax = (net - discount) * taxPercent ~/ 100;
return Price(total: net - discount + tax, tax: tax);
}
Future<Payment> charge(Customer customer, int total) async {
final payment = await gateway.charge(customer.card, total);
if (!payment.accepted) {
throw PaymentRefused(payment.reason);
}
return payment;
}

Set under the assay: key of the project’s root analysis_options.yaml.

Rule parameter Default Effect
max none in the rule; the presets give it The largest number of lines of code a function may have. A whole number, zero or more.
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.

The rule has no limit of its own. The presets give one, and where no limit is in effect, the rule reports nothing:

Preset max
core the rule is not in this preset
recommended 60
strict 40
assay:
rules:
max_function_length:
max: 80

recommended and strict also set this rule’s exclude to the test directories, test/**, integration_test/** and test_driver/**, read from the project root.

Write why on the line directly above the suppression. It goes above the function’s name, where the violation is, or at the end of that line:

// The steps of the migration are one transaction and are read top to bottom.
// ignore: assay/max_function_length
Future<void> migrate(Database database) async {

// ignore_for_file: assay/max_function_length silences it in a whole file, and // ignore: max_function_length without the prefix works too. See Suppressions.

All cases

Each of these is measured on its own:

  • a top-level function, a method, a getter, a setter, an operator;
  • a constructor with a body, factory constructors included;
  • a local function, declared inside another one;
  • a closure. It has no name, so the violation is on its parameter list: (item) in items.map((item) { … }).

A generated file is measured like any other: leave it out by its path, with exclude here or with assay.exclude for every rule.

The names that would say what each part of a long function is for were never written, and every change risks the parts around it. Code written by an AI coding agent grows long functions readily.

The length is the number of lines of the function’s body that carry code:

  • The body runs from its opening brace to its closing brace, both lines included. An expression body runs from => to the semicolon.
  • A line counts when at least one token of the body is on it. Blank lines and lines that hold only a comment do not count. A line with code and a comment after it counts.
  • What stands before the body is not counted: the documentation comment, annotations, return type, name, parameter list, and a constructor’s initializers.
  • Every line of a multi-line string counts, blank ones too.
  • A declaration with no body, such as an abstract method or an external function, has length 0.

So this function has length 5: the opening brace, three statements, the closing brace.

int total(int first, int second) {
// Start from the first.
var sum = first;
sum += second;
return sum;
}

A function on one line, int twice(int value) => value * 2; or void close() {}, has length 1.

A function declared inside another one counts for itself and for every function around it, since its lines are lines of the outer body. So a long closure gives two violations when the function that holds it is over the limit too.

In a test file, main and each group function hold every test below them, so their length says nothing about any one test. That is why recommended and strict exclude the test directories. The exemption covers the three directories at the project root: a test helper under lib/ or tool/, and the test directory of a package nested in the project, are judged.