Skip to content

avoid_long_files

v1.0.0WarningConfigurableCode Quality

This rule flags a file longer than the configured line budget.

A long file is where a module stops having a single subject: the name at the top no longer describes everything below it, and finding the right place to add something becomes a scroll rather than a decision.

Blank lines and comments are not counted by default — a file is not hard to navigate because it is well documented. Lines are counted from the token stream, so a // inside a string literal is never mistaken for a comment.

Generated files are the obvious exception, and per-rule exclude handles them:

rules:
avoid_long_files:
exclude: ["**/*.g.dart", "**/*.freezed.dart"]

This rule is in the pedantic preset: a budget is a house style, and the right number differs per codebase.

With max_lines: 10, one file carrying the model, its formatting and its transport:

class Invoice {
const Invoice(this.id, this.cents);
final String id;
final int cents;
}
String formatInvoice(Invoice invoice) {
final major = invoice.cents ~/ 100;
final minor = invoice.cents % 100;
return '\$$major.${minor.toString().padLeft(2, '0')}';
}
Future<Invoice> fetchInvoice(String id) async {
return Invoice(id, 0);
}

Split on the subjects the file already has — invoice.dart, invoice_formatting.dart, invoice_api.dart:

invoice.dart
class Invoice {
const Invoice(this.id, this.cents);
final String id;
final int cents;
}
invoice_formatting.dart
String formatInvoice(Invoice invoice) {
final major = invoice.cents ~/ 100;
final minor = invoice.cents % 100;
return '\$$major.${minor.toString().padLeft(2, '0')}';
}
invoice_api.dart
Future<Invoice> fetchInvoice(String id) async {
return Invoice(id, 0);
}

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

many_lints.yaml
rules:
avoid_long_files:
enabled: true
analysis_options.yaml
many_lints:
rules:
avoid_long_files:
max_lines: 300
count_comments: false
Option Type Default Description
max_lines int 300 How many code lines a file may hold
count_comments bool false Whether blank lines and comments count toward the budget

To disable this rule:

many_lints.yaml
rules:
avoid_long_files: false

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