Skip to main content

Spaced Out

White space is one element of code that is ignored by the compilers or interpreters of most computer languages. It has no programatic value at all. Yet it has an enormous impact on comprehension.

It is how we use it that ultimately governs how readable the code becomes. Proper indenting and use of blank lines can provide an immediate sense of the structure of any code.
Some simple rules, if followed consistently, will make this happen.

Separate declarations from code

If you declare a variable, even if you assign a value to it at the declaration time, separate it from other code by including a blank line and try to avoid declaring more than one variable on a single line:

private void PrintSomething(int value)
{
    string format = "{0;0.00}", formatInLine = "{0;#,0}";
    Console.WriteLine(format, value);
    for (int i = 0; i < 20; i++)
        Console.Write(formatInLine, value);
    Console.WriteLine();
}

Consider how much easier it is to figure out where format and formatInLine were declared, if we simply add a blank line after their declaration and declare each variable on a separate line:

private void PrintSomething(int value)
{
    string format = "{0;0.00}";
    string formatInLine = "{0;#,0}";

    Console.WriteLine(format, value);
    for (int i = 0; i < 20; i++)
        Console.Write(formatInLine, value);
    Console.WriteLine();
}

Separate blocks with empty lines

Each block of code should be preceded by a blank line to make it stand out. This should apply to any for , while , if or switch statement or similar:

You decide which of the following is more readable:

x += horizontalSpeed;
if (x > right) x = right else if (x < 0)  x= 0;
y += verticalSpeed;
if (y < top) y = top else if (y < 0) y = 0;

or:
x += horizontalSpeed;

if (x > right)
    x = right
else if (x < 0)
    x = 0;

y += verticalSpeed;

if (y > bottom)
    y = bottom
else if (y < 0)
    y = 0;

Of course, if a code block is the first and only sub-element of another code block, you would not put a blank line around the inner code block:

if (needToSendValues}
    for (int i = 0; i < valueCount; i++)
    {
    }

if (endOfStatement)
    switch(statementType)
    {
        case "invoice":
            // do something;
            break;

        case "purchase-order":
            // do something else
            break;
    }

return true;

Here it does not take us any effort at all to discern the if statements as separate entities.

Indenting

Just as it is important to vertically separate different code elements from each other, it is important to horizontally conditional code blocks.

So for any for , while , if or switch statements the body should be indented.

// this is hard to follow
x += horizontalSpeed;
if (x > right)
x = right
else if (x < 0)
x = 0;
y += verticalSpeed;
if (y > bottom)
y = bottom
else if (y < 0)
y = 0;

// this is easier to follow
x += horizontalSpeed;

if (x > right)
    x = right
else if (x < 0)
    x = 0;

y += verticalSpeed;

if (y > bottom)
    y = bottom
else if (y < 0)
    y = 0;

Brace placement

Braces or BEGIN / END keywords are used to mark the start or end of compound statements in most languages. Just like the statements themselves should be indented, so too should these braces or keywords.

It does not matter whether you are of the “trailing brace” or the “brace on new line” persuasion — just be consistent.

Similarly, it does not matter whether the braces line up with the containing statement or are indented with the code or not — again, just be consistent.