DEV Community 👩‍💻👨‍💻

Deathvenom
Deathvenom

Posted on

Tabs vs Spaces: I have been disillusioned 🤯

I have been steering away from any tabs/spaces article or conversation, for I considered this subject to be too trivial to debate on. I have been a stickler for 2 spaces since a while, and I don't care much about others' preferences.

So, I was under the impression that a tab is a character of a fixed size (6 or 8 spaces, perhaps). That's why I used 2 spaces, it fits more code on the screen.

Yesterday, when I was trying to quickly edit some stuff using Sublime Text (I rarely ever use it), I noticed it says at the bottom: Tab width: 4. I went to switch it to 2, I noticed another option, Use spaces instead. What? Up until that point, I believed that tab width refers to the number of spaces to indent code. That is when realization hit me.

Your code editor can display tabs to be of any length you want, it's not fixed! If you are working with someone on a project, and let's say they prefer 4 spaces and you prefer 2, just use tabs instead! You can configure your editor/IDE to display it as 4 and 2 respectively.

So that's it, I am a changed person now. I hope I cleared it out if a person who had the same stupid assumption I had read this post. Happy hacking!

Top comments (4)

Collapse
cappe987 profile image
Casper

There is one caveat with tab width, though. Line length. If person A uses 2-space tabs and has something indented 4 times that takes up 8 columns on screen. For person B using 8-space tabs that is now 32 columns. If you are working with a set line length, say 90 columns. Then if person A writes 90 columns, it will be far over 90 columns for person B. What fits fine on screen for person A is way off to the right for person B.

Tab width will also mess up alignment when doing line-wrapping like this (ignore the fact that I had to use all spaces because DEV does not accept tabs)

int this_is_some_function(struct some_person person1, 
                          struct another_person person2)
Enter fullscreen mode Exit fullscreen mode

For this reason, when working with the Linux kernel you should always use 8-space width tabs.

I was previously a fan of 2-space width with Use spaces instead. But after having to follow the Linux kernel standards at work I now really like 8-space width. It also helps you with not writing too long lines or too deeply nested. I'm not saying either is right or wrong, I'm still fine with using either. But is what I've learnt. Both have pros and cons.

Collapse
deathvenom54 profile image
Deathvenom Author

I see, I did not think about that....

Collapse
sharpweed profile image
rumnoodle

Imagine when we realize that indentation can be an editor feature altogether, surely if the editor can do all those neat things it can do today it can indent properly as well regardless of how the author did it.

In some languages they can do line breaks as well (to further mess with the poor ops who has to read the file in nano on the server)

Collapse
deathvenom54 profile image
Deathvenom Author

That sounds very plausible....you see it according to your settings, but the editor writes according to the existing indentation rules!

🌚 Browsing with dark mode makes you a better developer by a factor of exactly 40.

It's a scientific fact.