Skip to content

avoid_too_many_methods

v1.0.0WarningConfigurableCode Quality

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.

class UserManager {
// 25 methods: authentication, profile editing, avatar upload,
// notification preferences and CSV export, all in one class.
}
class UserAuthentication { /* ... */ }
class UserProfile { /* ... */ }
class UserAvatars { /* ... */ }

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

many_lints.yaml
rules:
avoid_too_many_methods:
enabled: true
analysis_options.yaml
many_lints:
rules:
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

To disable this rule:

many_lints.yaml
rules:
avoid_too_many_methods: false

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