avoid-if-with-many-branches
Warns when an if statement has too many branches.
⚙️ Config
Set max-number
(default is 3
) to configure the minimum allowed number of branches.
analysis_options.yaml
dart_code_metrics:
rules:
- avoid-if-with-many-branches:
max-number: 3
Example
❌ Bad:
void fn(int value) {
// LINT: This if statement has 4 branches, which exceeds the configured maximum.
if (value == 1) {
...
} else if (value == 2) {
...
} else if (value == 3) {
...
} else {
...
}
}
✅ Good:
void fn(int value) {
switch (value) {
case 1:
...
case 2:
...
case 3:
...
default:
...
}
}