#965 – Indent Code to Improve Readability

The C# compiler doesn’t care how consecutive statements in your source code are indented, or even whether they appear on one or on multiple lines.

For clarity, it’s customary to indent blocks of code to show visually that the code is within the scope of an outer block or statement.  Four spaces are typically used as an indent.  (Some people use TABs instead of spaces, but TABs are often displayed differently, depending on the editor and can make it difficult to change the indent level of a line).

Below is an example of code that uses indents with four spaces.

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Test(3);

            Console.ReadLine();
        }

        // Comment goes here
        static void Test(int i)
        {
            int twice = 2 * i;

            Console.WriteLine(i);
            if (i > 2)
                Console.WriteLine("not tiny");

            if (DateTime.Now.DayOfWeek == DayOfWeek.Thursday)
            {
                Console.WriteLine("Today is Thursday");
                Console.WriteLine("Eat {0} hamburgers", twice);
            }
        }
    }
}

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

Leave a comment