avoid_too_many_methods
This rule flags a class, mixin or extension declaring more methods than the configured budget.
Method count is the cheapest measure of whether a class still has one responsibility. A class with thirty methods is usually two or three classes that never got separated, and the tell is that its name has to be vague enough to cover all of them.
Getters, setters and operators are not counted by default. They are the class’s surface, not its behaviour, and a data class with fifteen getters is not the problem this rule exists for.
Constructors are never counted. A class with several named constructors is offering ways to build one thing, not doing several things.
This rule is in the pedantic preset.
With max_methods: 4, one class carrying four unrelated jobs:
class UserManager { void signIn(String email, String password) {} void signOut() {} void updateDisplayName(String name) {} void uploadAvatar(List<int> bytes) {} String exportCsv() => ''; // LINT: 5 methods exceeds the budget of 4}Split along the jobs the method names already name:
class UserAuthentication { void signIn(String email, String password) {} void signOut() {}}
class UserProfile { void updateDisplayName(String name) {} void uploadAvatar(List<int> bytes) {}}
class UserExport { String exportCsv() => '';}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: avoid_too_many_methods: enabled: trueOptions
Section titled “Options”many_lints: rules: avoid_too_many_methods: max_methods: 20 count_accessors: falserules: avoid_too_many_methods: max_methods: 20 count_accessors: false| Option | Type | Default | Description |
|---|---|---|---|
max_methods |
int | 20 |
How many methods a class may declare |
count_accessors |
bool | false |
Whether getters, setters and operators count |
Turning this rule off
Section titled “Turning this rule off”To disable this rule:
rules: avoid_too_many_methods: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”avoid_long_files— Keep a file within a line budget.avoid_long_parameter_list— Keep parameter lists within a budget.max_imports— Keep a file within an import budget.avoid_shadowed_extension_methods— An extension member the extended type already has.