Conditional operator nesting only allowed in false case

This is in reference to C++ static code analysis: Conditional operators should not be nested that triggers when nesting conditional operators, also known as ternary expressions.

I’m leaning in a direction where I would like to see a relaxed version of this rule where nesting is allowed in the second case in the ternary. I think code written in that way can be formatted well enough where any alternative is only more verbose with little benefit in terms of readability.

return condition1   ? expression1
       : condition2 ? expression2
       : condition3 ? expression3
                    : expression4;

Hi @torgeir.skogen,

I shared your code sample with a few colleagues, and the general sentiment was the following:

  • People who usually don’t see any issue with the conditional operator as long as it’s correctly formatted agreed with you (but those people already disable S3358, so would not benefit from this change).
  • People who don’t like nesting the conditional operator in general were not convinced that this case was really an improvement compared to the alternative of a list of if - else ifs.

As a consequence, since this rule is not about finding the best style that matches someone’s preference, but about finding the lowest common code that everybody finds acceptable, we do not plan to apply this suggestion.

I suppose what I was hoping for is that people in the first group would like to turn on the more relaxed rule. But I can respect that this is considered too narrow to warrant the extra cost of maintaining another rule.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.