Skip to content

Nested ternary operator (NestedTernaryOperator)

Type Scope Severity Activated
by default
Minutes
to fix
Tags
Code smell BSL
OS
Major Yes 5 brainoverload

Description

Use of nested ternary operators decrease code readability.

Examples

Incorrect use of ternary operators

Result = ?(X%15 <> 0, ?(X%5 <> 0, ?(X%3 <> 0, x, "Fizz"), "Buzz"), "FizzBuzz"); 
If ?(P.Emp_emptype = Null, 0, PageEmp_emptype) = 0 Then

      Status = "Done";

EndIf;

Possible implementation

If x % 15 = 0 Then
    Result = "FizzBuzz";
ElseIf x % 3 = 0 Then
    Result = "Fizz";
ElseIf x % 5 = 0 Then
    Result = "Buzz";
Else
    Result = x;
EndIf;
If PageEmp_emptype = Null OR PageEmp_emptype = 0 Then

      Status = "Done";

End If;

Snippets

Diagnostic ignorance in code

// BSLLS:NestedTernaryOperator-off
// BSLLS:NestedTernaryOperator-on

Parameter for config

"NestedTernaryOperator": false