Switch Default First Or Last
ID |
go.switch_default_first_or_last |
Severity |
info |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
Go |
Tags |
code-style |
Description
Reports a switch whose default clause is placed somewhere other than first or last among the
case clauses. Applies to both expression switches and type switches; select statements are not
considered.
Rationale
The position of default does not change behaviour, but readers expect the catch-all either at the
top (stated up front) or at the bottom (the fall-through of last resort). A default buried between
ordinary cases is easy to overlook and reads like a normal case, which hurts comprehension and
review.
switch n {
case 1:
// ...
default: // FLAW — neither first nor last
// ...
case 2:
// ...
}
Remediation
Move the default clause to the top or the bottom of the switch. Placing it last is the most
common convention.