Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-1301

"switch" statements should have at least 3 "case" clauses

    Details

    • Type: Code Smell Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      Replace this ["switch"|"CASE"|"Select Case"] statement with ["if"|"IF"|"If"] statements to increase readability.
    • Default Severity:
      Minor
    • Impact:
      Low
    • Likelihood:
      Low
    • Default Quality Profiles:
      Sonar way
    • Targeted languages:
      Cobol, PL/I, RPG
    • Covered Languages:
      ABAP, C#, C, C++, Flex, Java, JavaScript, Objective-C, PHP, Swift, TypeScript, VB.Net, VB6
    • Irrelevant for Languages:
      Go, HTML, PL/SQL, Python, T-SQL, XML
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Level:
      Syntactic Analysis
    • Analysis Scope:
      Main Sources, Test Sources
    • Common Rule:
      Yes
    • ESLint-SonarJS:
      no-small-switch
    • PC-Lint:
      764, 1764, 483, 1483
    • PMD:
      TooFewBranchesForASwitchStatement
    • TSLint-SonarTS:
      no-small-switch

      Description

      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();
      }
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                freddy.mallet Freddy Mallet (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated: