-
Type:
Code Smell Detection
-
Status: Active
-
Resolution: Unresolved
-
Labels:
-
Message:Reduce this "[switch/CASE|CASE/WHEN]" number of lines from {} to at most {}, for example by extracting code into a [method|subroutine|function].
-
List of parameters:
- key : max
- Description: Maximum number of lines of code
- Default value : 5
- Default for PHP: 10
- Default for T-SQL: 10
- Type: Integer
-
Default Severity:Major
-
Impact:Low
-
Likelihood:High
-
Legacy Key:too-many-loc-in-case-clause
-
Targeted languages:ABAP, Cobol, JavaScript, PL/I, TypeScript
-
Covered Languages:C#, C, C++, Flex, Go, Java, Kotlin, Objective-C, PHP, PL/SQL, RPG, Ruby, Scala, Swift, T-SQL, VB.Net, VB6
-
Irrelevant for Languages:HTML, Python, XML
-
Remediation Function:Constant/Issue
-
Constant Cost:5min
-
Analysis Level:Syntactic Analysis
-
Analysis Scope:Main Sources
-
Common Rule:Yes
-
PMD:SwitchDensity
The switch statement should be used only to clearly define some new branches in the control flow. As soon as a case clause contains too many statements this highly decreases the readability of the overall control flow statement. In such case, the content of the case clause should be extracted into a dedicated method.
Noncompliant Code Example
With the default threshold of 5:
switch (myVariable) { case 0: // Noncompliant: 6 lines till next case methodCall1(""); methodCall2(""); methodCall3(""); methodCall4(""); break; case 1: ... }
Compliant Solution
switch (myVariable) { case 0: doSomething() break; case 1: ... } ... private void doSomething(){ methodCall1(""); methodCall2(""); methodCall3(""); methodCall4(""); }
- is duplicated by
-
RSPEC-137 Avoid switch case clause with too many lines of code
- Closed
- is implemented by
-
SONARABAP-351 Rule: "switch case" clauses should not have too many lines of code
-
- Open
-
-
SONARCOBOL-1493 Rule: "switch case" clauses should not have too many lines of code
-
- Open
-
-
SONARPLI-168 Rule: "switch case" clauses should not have too many lines of code
-
- Open
-
-
CPP-395 Rule: Switch cases should not have too many lines
-
- Closed
-
-
SONARRPG-12 Rule "WHEN" clauses should not have too many lines"
-
- Closed
-
-
SONARSLANG-46 Rule S1151: "switch case" clauses should not have too many lines of code
-
- Closed
-
-
SONARSLANG-209 [Scala] Enable Rule: S1151 "match case" clauses should not have too many lines of code
-
- Closed
-
-
SONARSLANG-282 [Apex] Enable Rule: S1151 "switch case" clauses should not have too many lines of code
-
- Closed
-
-
SONARSWIFT-126 Rule: "switch case" clauses should not have too many lines
-
- Closed
-
-
SONARTSQL-44 Rule: "WHEN" clauses should not have too many lines of code
-
- Closed
-
-
SONARVB-283 Rule: "switch case" clauses should not have too many lines of code
-
- Closed
-
-
SONARVBSIX-183 Rule: "[switch|case|CASE|WHEN]" clauses should not have too many lines
-
- Closed
-
-
SONARVBSIX-245 Rule: "switch case" clauses should not have too many lines of code
-
- Closed
-
-
CPP-707 Create Objective-C rules repository containing targeted rules
-
- Closed
-
-
SONARABAP-207 Update title and description of rule "Switch cases should not have too many lines"
-
- Closed
-
-
SONARFLEX-87 Update "too many lines of code" rules to use LoC
-
- Closed
-
-
SONARPLSQL-489 Rule: "[switch|case|CASE|WHEN]" clauses should not have too many lines
-
- Closed
-
- is related to
-
CPP-2421 S1151: Fix FP linked to counting the outermost curly braces
-
- Closed
-
- links to