Why tabs are better than spaces

⚠️ Warning!

Opinions in this article may differ from your own. Beware of mild generalizations.

Background

I’ve been writing code for over 20 years in several languages and all types of editors, including: Notepad, Visual Basic, D…


This content originally appeared on DEV Community and was authored by Matt Kenefick

⚠️ Warning!

Opinions in this article may differ from your own. Beware of mild generalizations.

Background

I've been writing code for over 20 years in several languages and all types of editors, including: Notepad, Visual Basic, Dreamweaver, TextMate, Coda, Sublime, Xcode, Visual Studio, and others.

For the majority of that time, I've been pretty adamant about 4 spaces.

Why I might be changing my mind

In very recent years, the popularity of a few frameworks has spawned a rise of cases in 2-space indentation. In my opinion, this level of indentation is too low and creates confusion. My assumption is that 2-space indentation was adopted to save horizontal real estate, but I'm not entirely sure why that would be required.

After all, if your code has to be indented to the point it becomes unreadable, you likely have other code-related issues to fix. In fairly broad strokes (⚠️), you have indentation for the following:

namespace
    class
        function
            conditional
                loop
                    logic

There's more than enough room to keep code within an 80 character margin with a 4-gap indentation even at 6 levels.

But why tabs?

I'm not on a mission to stop people from writing 2-gap indentation. If that's what you like, that's what you like.

When I write my code, I want to see 4-gap indentation even if I'm sharing the codebase with 2-gappers. This creates a problem because formatting the file will add/remove spaces on every line to meet indentation levels which causes your git diff to change virtually every line; unreasonable and irresponsible!

Tabs make more sense because each individual tab represents one logical level of indentation.

You may have noticed I changed my terminology in this article to say "2-gap" rather than "2-space" and that's because it's the visual representation of space that matters; not the actual character. Even though spaces require more disk space.

Within your editor, you can change the visual representation of a tab:

Sublime Text Tab Representation

You can still get your 2-gap, 4-gap, 8-gap indentation level using tabs while working seamlessly with other developers. If Johnny is using 2-gap tabs, I'm using 4-gap tabs, and Maria is using 8-gap tabs... none of the actual characters change in our commits even though we can all feel comfortable with our preferred indentation sizes.

TL;DR

  • Each tab (\t / 0x09) represents one level of logical indentation, rather than having to compute/reduce spaces as a representation of indentation.

  • Tabs allow multiple developers to use their own indentation preferences without overriding formatting of others.

  • Forcing spaces of your own preference onto the other members of your team is a bit selfish.


This content originally appeared on DEV Community and was authored by Matt Kenefick


Print Share Comment Cite Upload Translate Updates
APA

Matt Kenefick | Sciencx (2021-07-27T15:20:31+00:00) Why tabs are better than spaces. Retrieved from https://www.scien.cx/2021/07/27/why-tabs-are-better-than-spaces/

MLA
" » Why tabs are better than spaces." Matt Kenefick | Sciencx - Tuesday July 27, 2021, https://www.scien.cx/2021/07/27/why-tabs-are-better-than-spaces/
HARVARD
Matt Kenefick | Sciencx Tuesday July 27, 2021 » Why tabs are better than spaces., viewed ,<https://www.scien.cx/2021/07/27/why-tabs-are-better-than-spaces/>
VANCOUVER
Matt Kenefick | Sciencx - » Why tabs are better than spaces. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/27/why-tabs-are-better-than-spaces/
CHICAGO
" » Why tabs are better than spaces." Matt Kenefick | Sciencx - Accessed . https://www.scien.cx/2021/07/27/why-tabs-are-better-than-spaces/
IEEE
" » Why tabs are better than spaces." Matt Kenefick | Sciencx [Online]. Available: https://www.scien.cx/2021/07/27/why-tabs-are-better-than-spaces/. [Accessed: ]
rf:citation
» Why tabs are better than spaces | Matt Kenefick | Sciencx | https://www.scien.cx/2021/07/27/why-tabs-are-better-than-spaces/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.