max-switch-cases
Same as max_switch_cases in Pyramid Lint
- Group
- Complexity
- Path
lint/complexity/max-switch-cases- Default
- Off, opt in
- Origin
- Pyramid Lint
- Rule
- 265 of 512
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 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
// 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');
}
}void handleValue(int x) { switch (x) {void processStatus(String status) { switch (status) {Valid
// 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');
}
}Configure max-switch-cases
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"complexity": {
"max-switch-cases": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/complexity/max-switch-cases: <reason>More complexity rules
- maximum-nesting-level
maximum_nesting_levelFlags a function whose control-flow nesting runs deeper than the configured level.
- no-boolean-literal-compare
no-boolean-literal-comparerecommendedDon't compare boolean values to boolean literals.
- one-member-abstracts
one_member_abstractsFlags abstract classes whose only member is a single abstract method.
- prefer-conditional-assignment
prefer_conditional_assignmentrecommendedFlags
if (x == null) { x = y; }with no else branch. - prefer-conditional-expressions
prefer-conditional-expressionsFlags an
if/elsewhose branches each reduce to a single return or a single assignment to the same target. - prefer-expression-function-bodies
prefer_expression_function_bodiesFlags block function bodies that consist of a single
returnof a value. - prefer-extracting-callbacks
prefer-extracting-callbacksflutterFlags inline block-body callbacks passed to Widget constructors.
- prefer-for-elements-to-map-from-iterable
prefer_for_elements_to_map_fromIterablerecommendedFlags
Map.fromIterable(x, key: ..., value: ...).