by Gordon Zhu, November 2024
What I am about to say is important if your programs are ever read by others. Of course, professional programmers have an audience, but there are additional cases people forget:
If you indent using spaces, it will look exactly the same on every device. But if you indent with tab characters, it could look different on every device. This is because users can customize how tabs appear on their devices.
In practice, this means:
Many programs will render tabs as 8 spaces by default. Even with a few levels of nesting, this can eat up all the horizontal space, which forces readers to scroll horizontally to even see the code.
Assume that you have the following file. You've indented the file with tab characters, and have configured each tab to show up as 2 spaces. This is what you would see on your computer.
function demo() {
// Nested 1 level
// Nested 2 levels
// Nested 3 levels
// Nested 4 levels
}
But then, you upload your commit to Github so that you coworker can code review it. On Github, tabs are shown as 8 spaces. Instead of displaying the way you intended, the outcome is that it's shown in a way nobody wants.
function demo() {
// Nested 1 level
// Nested 2 levels
// Nested 3 levels
// Nested 4 levels
}
Another serious issue with tabs is that if you choose to use them, you must take care to never indent with spaces. In theory, this sounds easy, but in practice it's hard to do, and leads to grosteque results. Most people are not capable of being that careful, no matter how hard they try.
Let's assume there exists a religious tab-lover, named Tabby. On Tabby's computer, tabs are configured to display as 4 spaces. Assume Tabby writes the following code. On Tabby's computer, both lines are indented at the same level. But Tabby has inadvertently mixed tabs and spaces.
// Intended with 1 tab
// Indented with 4 spaces
Now a coworker loads up Tabby's code. This coworker is also a tab-lover, but has his computer configured to display tabs as 2 spaces. This is what the coworker sees!
// Intended with 1 tab
// Indented with 4 spaces
The second line is nested below the first one! Now we no longer have just a stylistic difference, but we've unknowingly changed the semantic meaning of the code—catastrophically destroying readability.
Tab-lovers will say, but that's silly, nobody would mix tabs and spaces. To these people, I would suggest observing more programmers in the wild and becoming more acquainted with the concept of human error.
In environments where you don't have control over whitespace configuration (some online editors, Stackoverflow, etc), I recommend manually using the spacebar. Yes this is somewhat inconvenient, but these situations are not so common.
If you have control over your environment, you can configure your editor to insert spaces each time you press the tab key. Visual Studio Code does this by default—each time you hit the tab key, it will insert 4 spaces.
Inserting 2 or 4 spaces for each tab key press are the most common. For example, Python traditionally uses 4 spaces, while JavaScript usually goes with 2 spaces; either is totally fine.
Since whitespace is "invisible", it can be difficult to determine if you really are using spaces and not tabs. In Visual Studio Code, if you highlight all of the code, spaces and tabs will be shown differently. Spaces are represented with faint grey dots while tabs are represented by right arrows. All editors worth using have a similar feature.
For example, if you highlight the following program in Visual Studio Code, you'll see something like this (notice the dots and right-arrow):
••// I'm indented with two spaces
→ // I'm indented with a tab character, set to length 2
Anecdotally, from talking to programmer friends in the San Francisco Bay Area, most companies require spaces instead of tabs. This lines up with my experience—at Google, the style guide mandated that we use spaces.
As mentioned earlier, Visual Studio Code defaults to using spaces instead of tabs. Thoughtful defaults like this are likely another reason so many people are opting for spaces over tabs.