-
Type:
Code Smell Detection
-
Status: Active
-
Resolution: Unresolved
-
Labels:
-
Message:Convert this usage of the ternary operator to an "if"/"else" structure.
-
Default Severity:Major
-
Impact:Low
-
Likelihood:High
-
Legacy Key:ConditionalOperator
-
Covered Languages:C, C++, Java, JavaScript, Objective-C
-
Remediation Function:Constant/Issue
-
Constant Cost:5min
-
Analysis Scope:Main Sources, Test Sources
-
Checkstyle:AvoidInlineConditionals
While the ternary operator is pleasingly compact, its use can make code more difficult to read. It should therefore be avoided in favor of the more verbose if/else structure.
Noncompliant Code Example
printf("%s", (i > 10 ? "yes" : "no"));
Compliant Solution
if (i > 10) { printf("yes"); } else { printf("no"); }
- deprecates
-
RSPEC-1521 Avoid use of conditional operator
- Closed
- is implemented by
-
CPP-655 Rule: The ternary operator should not be used
-
- Closed
-
-
CPP-707 Create Objective-C rules repository containing targeted rules
-
- Closed
-
- links to
1.
|
JavaScript | RSPEC-1954 |
|
Active | Unassigned | |
2.
|
Java | RSPEC-2106 |
|
Active | Unassigned |