-
Type:
Code Smell Detection
-
Status: Active
-
Resolution: Unresolved
-
Labels:
-
Message:Extract this nested ternary operation into an independent statement.
-
Highlighting:
-
Default Severity:Major
-
Impact:Low
-
Likelihood:High
-
Default Quality Profiles:Sonar way
-
Targeted languages:ABAP, APEX, Flex, PL/SQL, Rust, Scala, T-SQL, TypeScript
-
Covered Languages:C#, C, C++, Java, JavaScript, Objective-C, PHP, Python, Swift, VB.Net
-
Irrelevant for Languages:Cobol, Go, HTML, Kotlin, PL/I, RPG, Ruby, VB6, XML
-
Remediation Function:Constant/Issue
-
Constant Cost:5min
-
Analysis Level:Syntactic Analysis
-
Analysis Scope:Main Sources, Test Sources
-
Common Rule:Yes
-
ESLint:no-nested-ternary
Just because you can do something, doesn't mean you should, and that's the case with nested ternary operations. Nesting ternary operators results in the kind of code that may seem clear as day when you write it, but six months later will leave maintainers (or worse - future you) scratching their heads and cursing.
Instead, err on the side of clarity, and use another line to express the nested operation as a separate statement.
Noncompliant Code Example
public String getReadableStatus(Job j) { return j.isRunning() ? "Running" : j.hasErrors() ? "Failed" : "Succeeded"; // Noncompliant }
Compliant Solution
public String getReadableStatus(Job j) { if (j.isRunning()) { return "Running"; } return j.hasErrors() ? "Failed" : "Succeeded"; }
- is implemented by
-
SONARPY-806 S3358: Add a message on the secondary location
-
- Closed
-
-
SONARABAP-361 Rule: Ternary operators should not be nested
-
- Open
-
-
SONARFLEX-132 Rule: Ternary operators should not be nested
-
- Open
-
-
SONARPLSQL-684 Rule: Ternary operators should not be nested
-
- Open
-
-
SONARTSQL-116 Rule: Ternary operators should not be nested
-
- Open
-
-
CPP-1645 Rule: Conditional operators should not be nested
-
- Closed
-
-
SONARJAVA-2068 S3358 Ternary operators should not be nested
-
- Closed
-
-
SONARPHP-747 Rule S3358: Ternary operators should not be nested
-
- Closed
-
-
SONARPY-252 Rule S3358: Conditional expressions should not be nested
-
- Closed
-
-
SONARSWIFT-346 Rule: Ternary operators should not be nested
-
- Closed
-
-
SONARVBSIX-267 Rule: Ternary operators should not be nested
-
- Closed
-
- relates to
-
SONARPHP-1115 Revise rule S3358 to exclude shorthand ternary operator
-
- Closed
-
-
RSPEC-134 Control flow statements "if", "for", "while", "switch" and "try" should not be nested too deeply
- Active
- links to
1.
|
C-Family: Conditional operators should not be nested | RSPEC-3778 |
|
Active | Unassigned | |
2.
|
JavaScript | RSPEC-3912 |
|
Active | Unassigned | |
3.
|
PHP | RSPEC-4331 |
|
Active | Unassigned | |
4.
|
C# | RSPEC-4419 |
|
Active | Unassigned | |
5.
|
Swift | RSPEC-4620 |
|
Active | Unassigned | |
6.
|
VB.NET: If operators should not be nested | RSPEC-4864 |
|
Active | Unassigned | |
7.
|
Python: Conditional expressions should not be nested | RSPEC-5581 |
|
Active | Unassigned |