Ternary Equal Branches

ID

csharp.ternary_equal_branches

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

CSharp

Tags

duplicate-code, reliability

Description

Reports a ternary expression cond ? a : b whose true and false arms are textually identical. The condition does not select between two distinct values: the result is the shared value regardless of how the condition evaluates.

Rationale

A ternary is meant to choose between two distinct expressions. When both arms are the same, the surrounding code reads as if a decision were being made, but no decision exists. Either an arm was meant to differ and the change was forgotten, or the ternary should collapse to the shared value.

int x = flag ? 1 : 1;             // FLAW — both arms are 1
int y = flag ? Compute() : Compute(); // FLAW — both arms call the same expression
int z = flag ? 1 : 2;             // OK — distinct arms

Remediation

Decide whether the arms were meant to differ. If yes, fix the diverging arm. If no, remove the ternary and keep the shared value: int x = 1;.