Skip to content

Coding Style Guide Tabs vs. Spaces

Kai Hofmann powerstat@web.de
18.02.2006

Style guide background

The usage of tabs or spaces to format source code is some kind of a holy war, as Jamie Zawinski describes in [1]. So this document will give some background information about using tabs and/or spaces. Within [3] an overview of the usage of tabs/spaces for some well known open source projects can be found.

Use one consistent style within a project

Within a project (or better for all projects) a consistent style should be used to format the source code.
This will avoid a lot of problems with (for example) comparision tools. Also a consistent style could be supported by using a "code beautifier tool".

Don't mix tabs and spaces within one file

You should never have a source code formatting of tabs and spaces mixed within one file, because this will lead into strange effects when changing the tab size (see [2]).

Using tabs as a space compressor does not bring speed to your code

Some people still think that using tabs instead of spaces will make their code faster during execution. This is not correct, because code that will be compiled has no spaces or tabs in it anymore during its execution time. Code that is interpreted is often "compiled on the fly" into an internal representation during execution. So the execution itself will not been faster, only the scanning process while reading the source code might be a very little bit faster. But nowadays scanners are so fast, that it might not be possible to measure the difference.

Using spaces will always result in the same formatting

When using spaces for the formatting of source code, the layout of the source code will be always the same independently of the setting of the tab-size. This means the layout is consistent for different development tools and editors as well as for different people.

When using the tab-key, be aware of the difference of soft- vs. hard- tabs

In [1] Jamie Zawinski describes the differences between the tab-key and the tab as ASCII byte #9. When using the tab key, this might insert "hard" tabs (i.e. ASCII byte #9) or "soft" tabs - i.e. the number of spaces that has been set within the preferences. Beware of the difference!

Different tab sizes

Different people will use different tab sizes for example one tab might be 2,4 or 8 spaces in width. Larger tab sizes will make code unreadable, because:

  • the eye can not find the start of the next line
  • when code is deeply indented, the start of line will be faster outside the screen

Formatting of code that flows about more than one line will be different for different tab sizes (inconsistency). The following three examples show the same code formatted for the three different tab sizes:


// Tab size = 2
class test
{
  int i;

  void test();
  {
    for (i = 0; i < 10; ++i)
    {
      printf('Hello World %d!',
        i);
    }
  }
}


// Tab size = 4
class test
{
    int i;

    void test();
    {
        for (i = 0; i < 10; ++i)
        {
            printf('Hello World %d!',
                i);
        }
    }
}


// Tab size = 8
class test
{
        int i;

        void test();
        {
                for (i = 0; i < 10; ++i)
                {
                        printf('Hello World%d!',
                                i);
                }
        }
}

Don't drink and drive

From [3] there is a citation:

[quote, Linus Torvalds]


Especially when you've been looking at your screen for 20 straight hours, you'll find it a lot easier to see how the indentation works if you have large indentations.


Everybody should know that when you are looking at your screen for more than 10 hours then you will make mistakes - more mistakes for every hour! That's because you get tired or more extremly drugs will influence you.

So my personal answer to Linus Statement here is:

Don't code when you are tired or on drugs!

because it's the same as

Don't drink and drive!

and beware us all that you are not coding on medical, self driving car or weapon software etc. when you are tired or on drugs!

There is no excuse!

References

Bibliography

  1. Jamie Zawinski. 'Tabs versus Spaces: An Eternal Holy War'. http://www.jwz.org/doc/tabs-vs-spaces.html. 2000.
  2. CyrusN. 'Tabs vs Spaces'. https://blogs.msdn.microsoft.com/cyrusn/2004/09/14/tabs-vs-spaces/. 2004.
  3. Jeff Davis. 'Tabs v Spaces'. https://web.archive.org/web/20090130003259/http://xarg.net/writing/tabs. 2002.