Thursday, September 8, 2016

Why use Spaces over Tabs for Indentation in Code Editors - Eclipse

When I started coding in Eclipse, I was not aware that by default Eclipse uses tabs for indentation and tabs can have varied with e.g. 1 tab can be equal to 2 spaces or 4 spaces or even 8 spaces. Sometimes, It's all up to you how you configure tabs in your code editor and other times just the mercy of the tool you don't know how to configure e.g. VI in UNIX. I only realize the problem when I found too many difference in a file while check-in into SVN. Apparently, other people was using different indentation (spaces) and that's why the file was showing so many differences when I reformatted them in Eclipse. This happens to many programmers, some pay attention, some didn't and go ahead with the check-in the code, only to revert it back later. There is no clear guideline upon which one is better and whether a programmer should use tabs or spaces, even the Clean Code doesn't  help here.

The programming community is also divided into two part, one favor use of tabs while other favor use of spaces, I am from the second part but I have my reasons why I think spaces are better than tabs for indentation and most important of them are that "space" represent true spacing. A tab can mean anything e.g. 2 space, 4 space or 8 space but space is a space. So you get the true sense of spacing while writing code or aligning them.

Here are a couple of more reasons in favor of using "space" over "tabs" which make sense, at least to me.

You cannot use tabs for alignment, and people align things because people view source files using several different tools, and getting everybody across all their respective platforms to set things up is onerous. When you mix tabs and spaces there's no way to get the alignment right.

Indentation is fine if that's all you ever use them for, but human behavior is non-deterministic and unenforceable, making for completely hosed up source when tabs are used for anything other than pure indentation. For example, if you put a tab in for the alignment when you should have used spaces when you copy this code to another text editor, the spacing will go wrong. Suddenly the code will become wider with more spacing all around.

There are so many IDEs, text editors, and build tools int programming world and they aren't smart enough to know when something is being indented vs. aligned, so you are always at risk of incorrect alignment when you use tabs for aligning your code.

Incorrect sense of Spacing
As I said before, Tabs are variable to hold spaces, it could be 2 space width, 4 space width or 8 space width and on some (albeit ancient) platforms won't even display. Spacing with spaces is more standard. Human nature is to believe what they see.

So if someone copies the code from Eclipse to text editors like TextPad or NotePad they expect to see the same indentation as they were seeing on Eclipse or NetBeans. You can reduce the pain by setting 1 tab = 2 spaces for less wide and 1 tab = 4 spaces which are most commonly used.

If sometime in the future a new formatting standard comes up it would probably deprecate tabs, but spaces I believe would always be a fallback option.

Why use Spaces over Tabs for Indentation in Code Editors - Eclipse

That's all about why spaces are better than tabs for indentation. It's not that tabs are completely bad but considering the vastness of programming community and tools, spaces are more standard and correct way to align and indent your code than tabs. If you work solo on a project that it doesn't matter if you stay consistent and keep using tabs but if you work on a project with multiple collaborators space is more natural than tabs. Though you can always pick one (spaces vs tabs) and stick with it by enforcing code reviews.

Further Learning
Beginners Eclipse Java IDE Training Course
Eclipse Debugging Techniques And Tricks
The Eclipse Guided Tour - Part 1 and 2

Other interesting articles for Java programmers


Csaba said...

Maaan. I could never understood those idiots using spaces. And still don't. After 10 years of dev experience, I haven't worked on any project, where the identation were spaces. It seems to me, as an old IT guy's habbit, whom was so stubborn to see the next gen.

Anonymous said...

Just wanted to say that in your second paragraph you wrote "one favor use of tabs while other favor use of tabs". You wanted to say spaces in the second choice as I can see. Thanks!

Javin Paul said...

@Anonymous, Indeed, thanks for pointing out.

Unknown said...

Tabs are 8 characters, the defaults on those IDEs are broken and the cause of the issue. If people used the 8 character tabs that were common on nearly every physical terminal it wouldn't be an issue.

Alessandro Carraro said...

Disclaimer: I'm for tabs. The reason is surprisingly not catched from the "spaces" lovers, but also from a number of "tab" lovers: the distinction between semantic and presentation. If you press a tab, EVERYONE, spaces or tabs lovers, knows that your code semantically intends one level. If you use 2 tabs, the code goes 2 level deep. While it is not a concern when leveling up (cannot find an example of code going 2 levels deeper) it is often when bubbling up, you need to understand how much you are going out the loops. 2 tabs less means 2 "blocks" closed.

When you do it with spaces, things could be the same IF people indented semantically with ONE space. But they do not. Because one space is not enough. But this is actually presentation layer. So some think 2 spaces are enough, other do 3, other 4. Just read one loves 8! Combine such programmers and you get a mess much worse than a file with tabs and spaces mixed together. So why press 4 times a spacebar to suggest one level lower? Why not use just a keystroke for the key designed to do that work? And the most beautiful thing has just to come: since using tabs semantics is always preserved, each programmer can then configure its personal IDE with the number of equivalent spaces he desires, and a bunch of people can have the presentation the way they like

stile said...

Maaan. I could never understood those idiots using tabs. And still don't. It seems to me, as an stupid IT guy's habbit, whom was so stubborn to see the next gen.

Post a Comment