Rules / Complexity / max-switch-cases
max-switch-cases
Flags a switch statement with more than the configured number of cases.
A switch with many cases is often better modeled as a map lookup,
polymorphism, or a sealed type with exhaustive handling. The rule counts
non-default (pattern) cases — the default clause is not counted — and
reports at the switch when the count exceeds the threshold. Nested switches
are checked independently.
Options
max_cases (integer, default: 10) — flag when the number of non-default
cases exceeds this.
Invalid
example.dartdart
// Bad: switch with 11 cases exceeds max (default: 10)
void handleValue(int x) {
switch (x) {
case 1:
print('one');
break;
case 2:
print('two');
break;
case 3:
print('three');
break;
case 4:
print('four');
break;
case 5:
print('five');
break;
case 6:
print('six');
break;
case 7:
print('seven');
break;
case 8:
print('eight');
break;
case 9:
print('nine');
break;
case 10:
print('ten');
break;
case 11:
print('eleven');
break;
default:
print('other');
}
}
// Bad: another switch with 12 cases
void processStatus(String status) {
switch (status) {
case 'active':
print('active');
break;
case 'inactive':
print('inactive');
break;
case 'pending':
print('pending');
break;
case 'processing':
print('processing');
break;
case 'completed':
print('completed');
break;
case 'failed':
print('failed');
break;
case 'cancelled':
print('cancelled');
break;
case 'paused':
print('paused');
break;
case 'queued':
print('queued');
break;
case 'error':
print('error');
break;
case 'warning':
print('warning');
break;
case 'info':
print('info');
break;
default:
print('unknown');
}
}
Switch statement has too many cases (max 10).
2void handleValue(int x) {
3 switch (x) {
∙
Switch statement has too many cases (max 10).
43void processStatus(String status) {
44 switch (status) {
∙
Valid
example.dartdart
// Good: switch with 9 cases within limit
void handleValue(int x) {
switch (x) {
case 1:
print('one');
break;
case 2:
print('two');
break;
case 3:
print('three');
break;
case 4:
print('four');
break;
case 5:
print('five');
break;
case 6:
print('six');
break;
case 7:
print('seven');
break;
case 8:
print('eight');
break;
case 9:
print('nine');
break;
default:
print('other');
}
}
// Good: switch with 10 cases (at the limit)
void handleStatus(int code) {
switch (code) {
case 200:
print('ok');
break;
case 201:
print('created');
break;
case 204:
print('no content');
break;
case 301:
print('moved permanently');
break;
case 304:
print('not modified');
break;
case 400:
print('bad request');
break;
case 401:
print('unauthorized');
break;
case 403:
print('forbidden');
break;
case 404:
print('not found');
break;
default:
print('other');
}
}
// Good: switch with 5 cases well under limit
void processType(String type) {
switch (type) {
case 'int':
print('integer');
break;
case 'double':
print('floating point');
break;
case 'string':
print('text');
break;
case 'bool':
print('boolean');
break;
default:
print('unknown type');
}
}
How to configure
Set the severity of max-switch-cases in your falcon.json:
falcon.jsonjson
{
"linter": {
"rules": {
"complexity": {
"max-switch-cases": "error"
}
}
}
}