The debate of tabs vs. spaces has raged on among software engineers for decades. It’s the closest thing the software industry has to a holy war and the casualties can be real. The tabs vs. spaces debate has ended partnerships, hurt feelings, and crashed programs.
The essence of this debate boils down to whether a programmer chooses to insert ASCII character 9 (alt
+ num 0
> num 9
) to insert whitespace into their code vs. using ASCII character 32 (alt
+ num3
> num2
). The debate sounds pedantic at first glance but there are some outstanding considerations that have fueled it for decades.
Quick Intro: Definitions
Before we dive into the tabs vs. spaces debate we need to agree on a shared vocabulary or terms. Part of the reason this debate continues is due to the ambiguity of certain terminology. Below are some key terms one needs to know and recognize to fully grasp the argument of each side:
- There is a Tab character and a Tab key.
- The Tab Character is the ASCII #9 character.
- The Tab Key is a piece of plastic on a keyboard.
- The Space Character is the ASCII #32 character.
Note: This printable ASCII table contains the tab, space, and every other of the first 255 ASCII characters with their HTML and Unicode values for easy reference.
Fun Facts
- Programmers that use spaces make more money than their peers that use tabs or those using a combination of tabs and spaces.
- Analysis of 400,000 Github repositories containing unique files written in one of several top languages found that spaces are, by far, the statistically preferred means of indicating whitespace.
- The emacs text editor uses 8-spaces for tabs.
The Issues: Interoperability, Visual Display, & Efficiency
The Tabs vs. Spaces debate can be boiled down to three primary concerns, into which most opinions can be categorized:
- Concerns for visual display of indentations within code editors.
- Software interpretation of the #9 ASCII character when found in disk files.
- What happens when the Tab Key gets pressed.
In essence, points #2 and #3 are more about system interoperability than opinion. These concerns are very real and, in many cases, the framework for arguments that win out for the case of using spaces.
As such, most of the “passion” behind the spaces vs. tabs debate can be attributed to point #1. It is here that we hear arguments for using spaces to allow more flexibility in indentation size. It is also here we hear arguments for using tabs because they can be more readily configured for custom indentation preferences. In either case, the debate gets messy.
How Many Spaces is a Tab?
Generally speaking, the common default amount of space inserted by ASCII character #9 is equivalent to inserting the ASCII character #32 4 times. Historically, this default was the equivalent of 8 ASCII #9 characters and, depending on one’s text editor, might still be the case. Some text editors like emacs still use 8 space characters per tab insert by default.
A “TAB” is an ambiguous term meaning “the shortest possible series of tab and space characters so as to align to the desired column,” according to the official Emacs documentation. This implies that — even when a default value is set for the TAB key — any given press could insert any amount of spaces based on the current position within the document.
As an example, Microsoft Word allows users to set custom column widths, as well as custom alignment options, for use with the Tab character. By adding an additional marker on the ruler section (via clicking) a user can instruct Word to increase or decrease the default 0.5″ column width between tabs.
For programmers, this isn’t often a cause for concern. Adjusting column widths for printouts is reserved for string formatting and is specified on a case-by-case basis. The extent to which the Tabs vs. Spaces debate is concerned about the Tab Keys’ functionality rests with the number of spaces it inserts — since tabs are usually just spaces in most modern IDEs.
Tabs are Usually Spaces Now
Most modern IDEs like Visual Studio Code or IntelliJ don’t insert a tab character when the tab key is pressed. Rather, they insert a predefined number of space characters needed to move the cursor to the next text column. A common default value is 4, though 8 and 2 are sometimes seen as well, depending on IDE. In this sense, it’s almost impossible to know for sure how many spaces is in a tab on any particular system. Safe guesses can be made — but ultimately one must recognize that anyone can select a custom number of spaces per tab.
For example, the PyCharm IDE (built on the IntelliJ platform) allows users to specify whether to use tabs or spaces and, for those electing to use the default spaces setting, how many spaces to insert.
PyCharm was chosen for an example here since the Python language interprets indentation to bear significance. Python can accept either tabs or spaces but, per the official Python documentation, will not tolerate a mixture of the two. The preferred choice is spaces where tab characters are used only to update files where the tab character already exists.
Language-Based Indentation
To further confuse things, most modern IDEs also change the default number of spaces to insert via a Tab key press based on the document type. For example, Visual Studio products (including Visual Studio Code) allow per-language settings. These include many settings other than tabs vs. spaces and how many spaces per tab. The following are common indentation examples where a Tab Key might insert a non-intuitive number of Space Characters.
*.html
files: sometimes 4 spaces per indent sometimes 2.*.css
or*.scss/*.sass
: Sometimes 2 sometimes 4. Usually 2 in compiled CSS and 4 in manually written. See here.- Shell/Bash: commonly 2 sometimes 4.
This is by no means an exhaustive list. In most cases, languages allow users to configure their preferred number of Tab Characters or Space Characters used for indentation. As such, a great myriad of coding styles has evolved over the years which are distinguished by their election of indentation style. To be clear — these differences are a matter of indentation style, not tabs vs. spaces.
Hearing Both Sides
There are valid arguments in favor of both Tabs and Spaces. The arguments in favor of Spaces has clearly swayed most developers — based solely on usage statistics. It is important to hear both sides of the debate — if for no reason other than to solidify ones own choice. Below are some common points of merit for the election of either tabs or spaces.
Pro Tabs
- Developed specifically to illustrate tabulation which, in many cases, is commonly used for indentation via moving the cursor to the first tabulated column.
- Allow developers wth different indentation preferences to change the display of their code without changing the actual code.
- There are no partial indentations using tabs whereas 3 spaces could accidentally be used to represent a single indentation instead of 4.
- File sizes are smaller given the alternative to a single tab character is often 4 space characters. While these are visually the same the byte-wise representation is not.
- Require fewer keypresses (legacy argument)
Pro Spaces
- Spaces are displayed visually the same across any system.
- Different systems interpret the tab character differently. It might result in more or less indentation than indented by the original developer when displayed on another system.
- The Tab character is often interpreted as “move to the next tab stop” which might be different, based on system configuration.
Pro Both
- Use Tabs for line indentation and spaces for intra-line spacing. This allows indentation preferences to easily be set without destroying the display of any specific line.
Noteworthy Opinions
The debate of tabs vs. spaces is, largely, one of opinion. While no amount of fame makes one’s opinion more objectively correct it does make it of more interest, generally. Below are the opinions of several noteworthy developers with respect to the tabs vs. spaces debate:
- Guido van Rossum (Python): Spaces.
- Jeff Attwood (StackOverflow): Spaces.
- Jamie Zawinski (Mozilla, Netscape): Spaces
Final Thoughts
The debate of tabs vs. spaces is the closest thing the programming world has to a holy war. Usage statistics clearly define the Spaces stance as having won out — notable exceptions being the Go language. In all cases, the fervor via which this debate takes place can be attributed to varying opinions on how indentation should be displayed. Aside from such opinions, the debate boils down to concerns of system interoperability.
Modern IDEs have alleviated the practical concerns — many replacing tab characters with spaces and even allowing custom spacing to be defined on a per-language basis. In this way, developers are empowered to choose whichever preference suits their coding style. The only point of agreement among those advocating for spaces and those advocating for tabs is this: pick one and don’t mix.