Skip to content

BSL251 — Ternary operator usage

Summary

Ternary operator usage

Identifiers

Field Value
Rule code BSL251
Compatible alias TernaryOperatorUsage
Severity INFORMATION
Enabled by default Yes
Implemented Yes
Tags style, readability

Behavior

  • The public identifier BSL251 and alias TernaryOperatorUsage are stable.
  • The rule reports the cases documented on this page.
  • Suppressions and project configuration are applied before publication.
  • The rule requires neither an external analyzer nor network access.

Configuration and suppression

BSL### is the primary stable identifier. The compatible alias is accepted in select, ignore, and compatible block suppression comments.

[tool.onec-hbk-bsl]
select = ["BSL251"]
ignore = ["TernaryOperatorUsage"]

All three suppression families support both a current line and a range. When an opening comment follows code, it affects only that line. Use any one form:

  • noqa:
Value = "example";  // noqa: BSL251
  • bsl-disable:
Value = "example";  // bsl-disable: BSL251
  • compatible BSLLS form:
Value = "example";  // BSLLS:TernaryOperatorUsage-off

When the same opening comment is on a line by itself, it starts a range. Close it with the matching marker from the same family:

// noqa: BSL251
// code without this diagnostic
// noqa-enable: BSL251

// bsl-disable: BSL251
// code without this diagnostic
// bsl-enable: BSL251

// BSLLS:TernaryOperatorUsage-off
// code without this diagnostic
// BSLLS:TernaryOperatorUsage-on

To disable the rule until the end of the file, omit the closing noqa-enable, bsl-enable, or BSLLS:…-on marker.

Opening and closing markers must belong to the same family.

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;