Coding Style Guide Tabs vs. Spaces
Kai Hofmann <hofmann@hofmann-int.de>
18.02.2006
Table of Content 1. Style guide background 2. Use one consistent style within a project 3. Don't mix tabs and spaces within one file 4. Using tabs as a space compressor does not bring speed to your code 5. Using spaces will always result in the same formatting 6. When using the tab-key, be aware of the difference of soft- vs. hard- tabs 7. Different tab sizes 8. References 1. 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. 2. 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". 3. 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 site (see [2]). 4. 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. 5. 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. 6. 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! 7. 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: - they 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 { <tb>int i; <tb>void test(); <tb>{ <tb><tb>for (i=0; i < 10; ++i) <tb><tb>{ <tb><tb><tb>printf('Hello World %d!', <tb><tb><tb><tb>i); <tb><tb>} <tb>} } // Tab size = 8 class test { <tabula>int i; <tabula>void test(); <tabula>{ <tabula><tabula>for (i=0; i < 10; ++i) <tabula><tabula>{ <tabula><tabula><tabula>printf('Hello World%d!', <tabula><tabula><tabula><tabula>i); <tabula><tabula>} <tabula>} } 8. References [1] Tabs versus Spaces: An Eternal Holy War. http://www.jwz.org/doc/tabs-vs-spaces.html [2] Cyrus' Blather Tabs vs Spaces http://blogs.msdn.com/cyrusn/archive/2004/09/14/229474.aspx [3] Tabs v Spaces http://xarg.net/writing/tabs