Coding Style GuideTopic 2 of 5~5 min

Indentation

Tabs vs. Spaces Debate Ends Marriage; Divorce Papers Cite 'Irreconcilable Differences'

Illustration comparing proper indentation as a staircase to success vs chaotic indentation

Dear Marilyn: Tabs or spaces? I need to know before I can continue living my life.

— Existentially Confused in Seattle

Dear Existentially: The answer is: tabs. But more importantly, the answer is: consistency. A codebase that mixes tabs and spaces is like a book that randomly switches between English and Klingon mid-sentence.

The Rules of Indentation

Rule 1: Use tabs, not spaces

Tabs allow each developer to set their preferred visual width. Spaces force everyone to see exactly what you see, whether they like it or not.

Rule 2: One level of indentation per block

Every time you open a brace, indent. Every time you close a brace, un-indent. This is not negotiable.

Rule 3: Align continuation lines

When a statement spans multiple lines, align the continuation with the opening element.

Visual Examples

Correct Indentation

public void ProcessOrder(Order order)
{
    if (order.IsValid)
    {
        foreach (var item in order.Items)
        {
            ProcessItem(item);
        }
    }
}

Incorrect Indentation

public void ProcessOrder(Order order)
{
if (order.IsValid)
{
foreach (var item in order.Items)
{
ProcessItem(item);
}
}
}

Without indentation, the code structure is invisible. Good luck debugging this at 2 AM.

Quick Check

Why is consistent indentation important in code?