DEV Community

Cover image for Indentation Warfare - Tabs or Spaces
Alex Kondov
Alex Kondov

Posted on • Originally published at alexkondov.com on

Indentation Warfare - Tabs or Spaces

I don’t remember when I found out about the timeless debate on tabs and spaces. I’m a self taught developer and I’ve missed some of the common arguments that occur in an academic environment.

My first job was quite forgiving for the uninitiated - no one was overzealous on engineering topics. After that I freelanced and worked for small startups. In that environment no time or energy could be spent on philosophical debates.

I think it was at my first real office job when I understood the gravity of the indentation topic. I casually joked that no one actually cares about this and it turned out to be false. I was faced with a wall of slightly raised eyebrows and looks of disappointment when I expressed my neglect to the topic.

Now, if I had said something in favour of either side I would’ve gotten some allies. But I managed to alienate both groups by playing down the seriousness of the matter.

As you can guess, I got educated on the matters of indentation warfare that day. Something that I thought was a matter of personal preference or habits, turned out to be a nuanced topic with solid reasoning from both sides.

If you’re thinking like I was and you’re wandering what the big deal is, let me spare you a lecture or two with this article. I’ve never thought I’d be writing about whitespaces but the peculiarities of this industry are endless.

The Conflict

We use indentation to create visual hierarchies and improve the structure and readability of our code. Mainly we use it to show what lines of code belong inside a conditional, loop, method or class.

Besides showing scope, it can also be used to break down longer logical operations. A ternary operator with a complicated condition can have its possible expressions on the next couple of lines indented to easily distinguish them.

Chained method calls, if they are more than two, can be indented on new lines to show that they belong to the same flow of operations.

Indentation is a tool that is useful only for the human - the developer reading and writing the code. The compiler benefits nothing from the amount of white spaces used at the beginning of each line (Python is an exception).

The argument about tabs and spaces is not about the correct amount of indentation. It’s about the characters we use and their benefits and drawbacks.

The Tab Character

Pressing the tab button on the keyboard outputs a tabulation character. It is interpreted by the editor as an amount of white space, usually 4 or 8 spaces.

Files that are formatted with tabs are usually smaller, since a single tab character represents multiple whitespaces. Not that it’s much of a problem nowadays.

The tab length is interpreted differently in IDEs and editors. It is also configurable, so if you would like to have tabulations of 6 spaces - you can. This makes tabs a great tool for people with visual impairments - they are customizable.

Tabs are descriptive - they tell the editor the amount of indentation that should be added. But the decision about how to render them depends on who reads the code. In the mind of a tab user, they should be used for indentation and spaces should be used for alignment.

The Argument for Spaces

The main arguments for spaces is the consistency. Different platforms and editors display tabs with varying sizes. Since they are configurable, there is no guarantee that the code you’ve written will be read the same way.

You’ve probably copy/pasted code from somewhere and it appeared all messed up because of differences in indentation.

Across all editors and platforms - a whitespace is always a whitespace. While tabs can be configured, the whitespace is the same no matter where it is shown. This means that the code will be displayed with the same formatting no matter the tab settings.

The difference is that this decision is only made by the writer of the code, not the reader. Spaces bake the formatting decisions in the code itself. Reading code with different formatting does not change its behaviour but it’s not the way the developer meant it to be read.

Not Just a Technical Debate

In the begining of the article we said that the argument is about the characters used to create indentation. But the debate is not just technical - it’s a philosophical one.

It’s about whose responsibility it is to specify indentation.

Tabs supporters believe that should be left to the readers - let them decide the structure. Spaces advocates would prefer to specify them explicitly - consistency is most important.

To me this is what all arguments and debates on the topic culminate in if we take personal preference aside.

Summary

With each article I get more fascinated about the depth of the software development industry. The fact that we can have broad and ongoing discussions about things as basic as white spaces.

You will meet zealots on both sides who will try to persuade you to join their ranks. They have solid arguments but in the end it comes to the preferences of the developers. It comes down to what you believe is right - should the author of the code specify its indentation or should that be left to the reader.

If an approach was outright wrong, it would’ve been ruled out by now. The fact that the debate is still on means that the truth is up to us to decide.

Top comments (1)

Collapse
 
colinintj profile image
Colin Fiedorowicz • Edited

Nice article! I used to indent with tabs, but now I use spaces. Until now, I thought people only preferred tabs because they're more convenient to type—at least that was why I used them. I began using spaces once I noticed how tabs appear differently in different editors.