Indentation

General

If you're using Visual Studio 2005 (or newer) you will need to set Indenting to Block under options. Indentation should be used to increase clarity and keep code within a normal width. Just because you have a 24" wide LCD doesn't mean everyone else should have to scroll to the right to read your code.

DO use indentation to:

  • To offset code within brace pairs. example
  • When continuing long code statements to the next line. example
  • To break up long argument lists onto multiple lines. example

Examples

Indenting Code Within Brace Pairs

Good

public class Thingy
{
    public Thingy()
    {
        //Indentation helps clarify what code belongs
        //to what class elements
        Console.WriteLine("This is good form");
    }
}

Bad
public class Thingy
{
public Thingy()
{
//Indentation helps clarify what code belongs
//to what class elements
Console.WriteLine("This is good form");
}
}

Continuing Statements to the Next Line

Good

public class Thingy
{
    public Thingy()
    {
        //Break up long statements into multiple lines.
        //This makes for easier reading, but failure
        //to properly indent reduces clarity.    
        Console.WriteLine("This is a very long line of code, I should probably" +
                            "break it up into multiple lines.");
    }
}

Bad
public class Thingy
{
    public Thingy()
    {
        //Break up long statements into multiple lines.
        //Notice that this is far less 'clear'?    
        Console.WriteLine("This is a very long line of code, I should probably" +
        "break it up into multiple lines.");
    }
}

Spreading Arguments Across Multiple Lines

Good

public class Thingy
{
    public Thingy()
    {
        //Break up long lists of arguments into multiple lines
        //to improve clarity and remember to line them up using indentation.
        something.RidiculouslyLongMethodCall(argument1, 
                                                argument2, 
                                                argument3, 
                                                argument4, 
                                                argument5, 
                                                argument6);
 
        //This is also acceptable
        something.RidiculouslyLongMethodCall(argument1, argument2, argument3, 
                                                argument4, argument5, argument6);
    }
}

Bad
public class Thingy
{
    public Thingy()
    {
        //Calls like this can cause those with smaller editing space headaches
        //DON'T keep long calls like this on one line.
        something.RidiculouslyLongMethodCall(argument1, argument2, argument3, argument4, argument5, argument6);
 
        //Poor indentation doesn't do much to improve it though...
        something.RidiculouslyLongMethodCall(argument1, argument2, argument3,
        argument4, argument5, argument6);
    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.