Control Statement Braces
ID |
csharp.control_statement_braces |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Best Practice |
Language |
CSharp |
Tags |
best-practice |
Description
Reports if, else, while, for, foreach and do control statements whose body is a
single statement written without surrounding braces.
Rationale
An unbraced control-statement body is fragile. When a second statement is added later it silently falls outside the control flow even though the indentation suggests otherwise — the "goto fail" family of bugs. Always bracing the body also keeps debug-stepping aligned with the condition and removes any ambiguity about which statements the branch or loop covers.
if (ready)
Start(); // FLAW
while (HasNext())
Advance(); // FLAW
for (int i = 0; i < n; i++)
Process(i); // FLAW
if (ready)
{
Start(); // OK
}