Skip to content

Ternary operator usage (TernaryOperatorUsage)

Type Scope Severity Activated
by default
Minutes
to fix
Tags
Code smell BSL
OS
Minor No 3 brainoverload

Description

Instead of the ternary operator, use the "If-else" construct.

Examples

Bad:

Result = ?(X%15 <> 0, ?(X%5 <> 0, ?(X%3 <> 0, x, "Fizz"), "Buzz"), "FizzBuzz"); 

Good:

If x% 15 = 0 Then
    Result = "FizzBuzz";
ElseIf, if x% 3 = 0 Then
    Result = "Fizz";
ElseIf, if x% 5 = 0 Then
    Result = "Buzz";
Else
    Result = x;
EndIf;

Bad:

If ?(P.Emp_emptype = Null, 0, P.Emp_emptype) = 0 Then
      Status = "Done";
EndIf;
Good:

If P.Emp_emptype = Null OR P.Emp_emptype = 0 Then
      Status = "Done";
End If;

Snippets

Diagnostic ignorance in code

// BSLLS:TernaryOperatorUsage-off
// BSLLS:TernaryOperatorUsage-on

Parameter for config

"TernaryOperatorUsage": false