Tabs or spaces? Spaces, obviously, but how many?

If you're passionate about code indentation, you're not alone.
247 readers like this.
A bunch of question marks

Opensource.com

Depending on who you ask, it's either the silliest or the most important question when it comes to coding style: tabs or spaces?

When we asked you, our readers, the question last summer, spaces came out on top. That said, it's a nuanced question where things like programming language, editor, and project style all should play a role.

Regardless of where you come down on the spaces versus tabs debate, there's a related question: If you use spaces, how many do you use, and if you use tabs, what number of spaces do you set to be equivalent to the depth of one tab level?

There's an argument for using as few as possible, particularly when the code you're writing includes particularly verbose naming conventions that drag the total number of characters per line quickly towards the triple-digits. Or perhaps you've got particularly deep levels of indentation in your code, and for whatever reasons aren't breaking longer code blocks up into subroutines.

The argument for using more spaces is also readability, but on the other side: It's sometimes hard to easily tell the indentation depth of a line of code if you don't use enough spacing, particularly with smaller font sizes.

So we ask you, dear reader, how many spaces equal one level of indentation in your editor of choice?

User profile image.
Opensource.com publishes stories about creating, adopting, and sharing open source solutions. Follow us on Twitter @opensourceway.

7 Comments

First of all, spaces in 2018? Why subject yourself to hundreds of thousands of extra characters unnecessarily?

I know, right? Tabs are better, IMO; the fact that you can configure your editor to match the amount of spaces you want each tab to represent makes them much better. Add to that that I can just tab once and get four spaces.... I honestly don't see the point of this endless tabs vs. spaces war when tabs have shown their superiority time and time again. Even the Go language uses tabs by default!

In reply to by D (not verified)

Tabs or spaces? TABS, obviously, but how many? simple answer with tabs: 1! Why use multiple spaces, tho Tab was invented a while ago...

And I see that author of this article is hidden under opensource.com name... I think Author is afraid, that someone will check his Writer with Paragraph mark on and will see spaces used for formatting :)

Tabs. Programmers can set the tab stop in their editors to how many spaces they prefer to see. Code does not get weird indentation because different programmers use different number of spaces. And the level of indentation is clear.

I'll use both tabs and spaces for HTML, Javascript and CSS without even thinking about it - because who cares, the whitespace is just a visual tool, right? And I'm not really writing anything that's to be widely shared across different editors.

With Python, just spaces.

Spaces. When collaborating with different people everybody has their editors configured differently. When tabs are used, files will display differently for different users. When tabs and spaces are intermixed, it's even worse - columns don't line up, making it difficult for anyone to read without configuring their editor for the same tab width as the original author.

As for saving bytes, I can't believe anybody cares, given the size of files and the size of storage media. As for convenience, I can still type the TAB key to insert the correct number of spaces (or re-indent the line). Emacs has done this for decades and I assume any decent programmer's editor can also do this.

Spaces.
Except for Makefiles, where tabs are required.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.