Skip to content

Latest commit

 

History

History
36 lines (28 loc) · 797 Bytes

no-small-switch.md

File metadata and controls

36 lines (28 loc) · 797 Bytes

no-small-switch

switch statements are useful when there are many different cases depending on the value of the same expression.

For just one or two cases however, the code will be more readable with if statements.

Noncompliant Code Example

switch (variable) {
  case 0:
    doSomething();
    break;
  default:
    doSomethingElse();
    break;
}

Compliant Solution

if (variable == 0) {
  doSomething();
} else {
  doSomethingElse();
}

See

  • MISRA C:2004, 15.5 - Every switch statement shall have at least one case clause.
  • MISRA C++:2008, 6-4-8 - Every switch statement shall have at least one case-clause.
  • MISRA C:2012, 16.6 - Every switch statement shall have at least two switch-clauses