Same Implementation Conditional
ID |
csharp.same_implementation_conditional |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
CSharp |
Tags |
duplicate-code, reliability |
Description
Reports an if / else whose then and else bodies are textually identical. When both
branches do the same thing, the condition contributes nothing and the code most often
reflects an unfinished edit or a copy-paste mistake.
Rationale
A pattern of the form if (cond) { X; } else { X; } collapses to just X;. The condition
is misleading: a future maintainer reading the code expects the branches to differ. Either
one of the branches was meant to behave differently and the change was forgotten, or the
duplication should be removed.
if (force) // FLAW — both branches do the same thing
{
Refresh();
}
else
{
Refresh();
}
if (force) // OK — distinct branches
{
HardRefresh();
}
else
{
Refresh();
}
Remediation
Decide what each branch is supposed to do. If they were genuinely meant to differ, fix the
diverging branch; otherwise remove the if altogether and keep a single statement.