Coding Style GuideTopic 4 of 5~5 min

Statements and Braces

Missing Brace Causes Production Outage; Developer Insists 'It Looked Fine to Me'

The humble curly brace is the unsung hero of code structure. Use it wisely, use it consistently, and it will never betray you. Omit it "to save space," and it will destroy your weekend.

The Brace Placement Rules

Rule: Opening brace on its own line

if (condition)
{
    DoSomething();
}

This is the C# standard. The brace aligns with the statement it belongs to.

Never: Omit braces for single statements

// DANGEROUS - Don't do this!
if (condition)
    DoSomething();
    DoSomethingElse(); // This ALWAYS runs!

This is how bugs are born. The second line looks indented, but it's not part of the if statement. Always use braces.

Statement Guidelines

  • One statement per line. Never chain multiple statements on one line.
  • One declaration per line. Don't declare multiple variables together.
  • Blank line between logical sections. Group related code visually.

Quick Check

Why should you always use braces even for single-line if statements?