Wednesday, May 17, 2023

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 at the mercy of the tool you don't know how to configure like VIM in UNIX. I only realize the problem when I found too many differences in a file while check-in in into SVN. Apparently, other people were using different indentations (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 of 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 the use of tabs while the 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 the most important of them is that "space" represents true spacing. 

A tab can mean anything like 2 spaces, 4 spaces, or 8 spaces but space is a space. So you get the true sense of spacing while writing code or aligning them.

Btw, if you are a beginner, I suggest you first go through these Eclipse IDE courses for Beginners to understand the core concepts of Eclipse IDE and get yourself familiar with UI and essential features. Learning plugins will be a lot easier after that.

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

1. Alignment

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 a 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 in the 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.


2. 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 the programming community and tools, spaces are a more standard and correct way to align and indent your code than tabs. 


Other interesting articles for Java programmers
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.

6 comments:

  1. 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.

    ReplyDelete
  2. 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!

    ReplyDelete
  3. @Anonymous, Indeed, thanks for pointing out.

    ReplyDelete
  4. 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.

    ReplyDelete
  5. 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

    ReplyDelete
  6. @Csaba
    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.

    ReplyDelete