Tabs vs. Spaces

Posted by Goombert on Aug. 31, 2015, 8:42 p.m.

Yes, that age old debate. This post in no way attempts to elicit a flamewar of programming fundamentalism.

I recently started a project to create a docking library for Oracle's new RIA platform, JavaFX. After years of banging my head against the wall with the default Eclipse formatter, I realized there had to be a better way. A colleague of mine, Josh Ventura, introduced me to the Google style guide and its elegant formatting of what is an otherwise Salvador Dali of semantics (Java).

After some research of this age old debate I really started to settle on spaces; having configured Notepad++ and Eclipse to use an 80 character width line limit and 2 spaces. After more research and just stewing on the idea, I believe my friend DaSpirit's earlier influence has begun to drive me in the opposite direction, and allow me to explain why.

Most of you who would read this are probably aware of the existing advantages and disadvantages of each. For example:

* Spaces are better with large projects and source revision control and are consistent with every editor (including the web)

* Spaces make it easier to detect formatting errors

* Spaces are semantically correct for alignment

* Tabs have smaller file sizes and thus lead to the computer even consuming less energy

* Tabs are quicker to type (not true if you properly configure your editor, but still almost always true for backspacing)

* Tabs are semantically correct for block indentation

* Tabs are dynamic and represent the separation of view as in MVC (you can set your editor to how you want to perceive or "view" the tabs/indentation)

However, I am starting to see things a little differently. We are all most likely aware that tabs can solve a lot of the complaints against it by proper configuration of the code editor. A lot of editors like Qt or Notepad++ also allow you to display a symbol for the tab character making it both easier to visualize (imo) and easier to detect SCM (like git or mercurial) formatting inconsistencies from other contributors.

I also feel that using tabs makes it easier to port my code. For example, let's say I prefer 3 spaces, my employer prefers 2 spaces, and I have a second job or hobby that requires 4 spaces. Through the use of tabs I am able to write my code once and run a regular expression to give it to any employer or publish it in a variety of different ways. Because I feel that this addresses all outstanding concerns related to tabs, I am still conflicted on the subject because of what I feel is the only remaining disadvantage to tab indentation. To clarify, I believe a mixture of tabs and spaces is where I am heading for my own personal projects as in the following screenshot:

If you've ever tried to publish code through the internet using http://pastie.org/ or to a forum/blog site like 64Digits you will understand what I mean. Websites where formatting code is virtually integral to the function of the site, such as pastie.org or Stack Overflow, have always been a headache for me with tabs. From the complete absence of tab input to the ugly line wrapping, using tab indentation on webpages has been a huge nightmare. However, many sites do work around this and provide tab input support, it still does not address the issue of configuring the tab width to a reader's liking (as far as I am aware). I feel this is extremely necessary for people to read the code the way that they want, specifically other people reading my code in a way that makes them comfortable. I would say that it is almost integral to the web.

In a perfect world we probably wouldn't use either and all of our code might be written on a single line with no whitespace with the editor configured to generate different views based on some parsing or lexical context. I would be very happy in that world as everyone would get a different view of the code just like with model view controller, which we sort of get with tabs. I am really looking to see what others have to say about this and if you feel my opinions are justified. Perhaps there is some browser configuration that could solve my tab problems on the web, or there is some other advantage to spaces that I have not considered. Either way I would like to know what others think and if anybody may have a solution to my dilemma. Comments welcome.

Comments

aeron 9 years, 3 months ago

Ideally we'd have something like elastic tabstops in all our editors so the concept of indention can be totally abstracted.

Goombert 9 years, 3 months ago

Wow aeron, that is precisely the kind of objective feedback I was looking for. This really does seem like the solution, thank you for sharing! It really makes me feel less hopeless about this dilemma. What's depressing though is looking around and seeing it being proposed way back in 2005 but never really catching on and the Eclipse plugin never becoming realistic. :-)

The only thing is that this still wouldn't fix the related web issues with tabs *right now* until more editors took the approach, which is my only concern with tabs. However this has really driven me further in the tabs usercamp.

Edit: I have discovered the Tabinta plugin for Firefox and also that you're in fact able to change the configuration of a tab width in the browser. This has practically sold me on using tabs.

https://addons.mozilla.org/en-us/firefox/addon/tabinta/

F1ak3r 9 years, 3 months ago

I have a section in my .vimrc where I put different indentation settings for different sorts of files as necessary, usually according to the official style for the language. Depending I can have it automatically turn tabs into spaces or just set how wide tabs should look, though I usually do the former. But I can change them when necessary as well.

autocmd FileType html setlocal shiftwidth=2 tabstop=2 expandtab
autocmd FileType ruby setlocal shiftwidth=2 tabstop=2 expandtab
autocmd FileType jade setlocal shiftwidth=2 tabstop=2 expandtab
autocmd FileType scss setlocal shiftwidth=2 tabstop=2 expandtab
autocmd FileType css setlocal shiftwidth=2 tabstop=2 expandtab
autocmd FileType python setlocal tabstop=4 shiftwidth=4 expandtab
autocmd FileType javascript setlocal tabstop=4 shiftwidth=4 expandtab

So I don't really have a significant preference for one over the other. I tend towards smaller indentations because I like to be able to have a lot of files open all at once and so like them to not be unnecessarily wide, but you can achieve that with either.

KaBob799 9 years, 3 months ago

There is tab-size in css3 but I've never actually tried it. I don't know how you would enable that on somebody else's website but I imagine there is a way. It's only a solution in some areas though, others will still display tabs wrong.

DesertFox 9 years, 3 months ago

@aeron What a coincidence! I found myself at that exact page earlier today and was just about to post it here.

Phoebii 9 years, 3 months ago

I use tab.

Why? I don't know. Too minor thing to care about.

Jani_Nykanen 9 years, 3 months ago

I use tabs since my code is not supposed to be seen by anyone but me. Plus using tabs is faster.

Astryl 9 years, 3 months ago

I use whatever I feel like, depending on the situation. Some scripting languages have a hard-coded requirement of spaces over tabs.

Generally, consistency is all that matters. The tab control character has been standard for decades, and if your editor/IDE/Compiler doesn't support it, screw it - that's a sign of a poorly designed editor anyway.

DFortun81 9 years, 3 months ago

I use tabs and smite those that choose otherwise.

You might not think it a problem, but interpreted/semi-interpreted languages (such as unoptimized C#, XML, Lua, HTML, JavaScript, etc) need to be downloaded and then parsed at runtime. The fewer the characters, the faster the interpretation occurs. (Saving micro seconds in some cases, but any amount of savings for something as simple as a tab vs. a number of space characters is well worth it.)

I once received an 8 MB XML file that was written with spaces instead of tabs from another programmer and then I converted the spaces to tabs and cut it down to 5 MB. The engineer and I had a brief discussion and after some hardcore bitch slapping and rework, we were able to get the file size down to about 120 KB. Array elements being stored as individual XML tags instead of a singular merged element were mostly to blame. This made the difference between using tabs and spaces that much more obvious and a no-brainer solution.