DEV Community

Cover image for Code Smell 211 - Tab over Spaces
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

Code Smell 211 - Tab over Spaces

Tabs or spaces, are equivalent?

TL;DR: Don't use Tabs. It is not a "personal style decision"

Problems

Solutions

  1. Use spaces. Always.

  2. Use automatic tools to prevent tabs in the code.

Context

Developers might see using tabs or spaces for indentation as a matter of personal preference or team convention.

it is generally recommended to be consistent with the chosen method of indentation within a project.

There are a few advantages of using spaces over tabs.

Spaces will always look the same, no matter the text editor, font spacing, or IDE used.

Tabs can vary in width, which can lead to inconsistent indentation when code is viewed on different platforms or in different editors.

Spaces are more consistent in terms of alignment and readability, particularly when it comes to code that involves a mix of spaces and tabs.

Spaces are more predictable and easier to read, which can help to reduce errors in code.

Some screen readers and other assistive technologies may have difficulty reading code that uses tabs for indentation, particularly when tabs are used inconsistently or when tab width is not uniform.

Sample Code

Wrong

def calculate_average(numbers):
    total = 0
    count = 0
        for number in numbers:
    total += number
            count += 1
        average = total / count
    return average

numbers = [1, 2, 3, 4, 5]
print("The average is:", calculate_average(numbers))

Enter fullscreen mode Exit fullscreen mode

Right

def calculate_average(numbers):
    total = 0
    count = 0
    for number in numbers:
        total += number
        count += 1
    average = total / count
    return average

numbers = [1, 2, 3, 4, 5]
print("The average is:", calculate_average(numbers))

Enter fullscreen mode Exit fullscreen mode

Detection

[X] Automatic

We can enforce a policy to avoid tabs.

Tags

  • Standards

Conclusion

Bad indentation can make the code difficult to read and understand and can cause errors if the indentation is not consistent throughout the code.

Using spaces for indentation is generally recommended for consistency, readability, and accessibility.

Relations

Disclaimer

Code Smells are my opinion.

Credits

Photo by Faisal Waheed on Unsplash


It is hard to write even the smallest piece of code correctly.

Joshua Bloch


This article is part of the CodeSmell Series.

Top comments (12)

Collapse
 
cloutierjo profile image
cloutierjo

How to start a flame war labeled as code smell.

Collapse
 
virtualmachine profile image
ByteCodeProcessor

Last time I used the IDLE that came with python i used tabs and everything worked fine. I did a quick Google search and the IDLE changes tabs to spaces. It's 2023 after our Lord, your text editor should be able to change tabs to spaces by default.

Collapse
 
mcsee profile image
Maxi Contieri

No flame war. Just polite arguments

Collapse
 
moopet profile image
Ben Sinclair
Collapse
 
jonrandy profile image
Jon Randy 🎖️

Tabs vs. Spaces: It's an Accessibility Issue • Adam Tuttle

Tabs cost you nothing and give your team more options. They're the more accessible choice.

favicon adamtuttle.codes
Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

It's not just a11y; different people just find different indentation widths the most comfortable to read and I don't want to force anyone looking at my code to be weirded out by my 3-space indentation preference.

Collapse
 
mcsee profile image
Maxi Contieri

Accessibility is a very good point

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

 Spaces will always look the same, no matter the text editor, font spacing, or IDE used.

This is a disadvantage. Code is not a fucking painting, and wanting everyone reading it to experience it the same way is nothing more than thinly veiled arrogance.

All the other non-arguments are just variations of this or the dishonest attempt to blame anything caused by mixed indentation on tabs. Spaces aren't immune to mixed indentation either; I constantly find files at work (we use spaces as company policy) where someone has committed a change with tabs.

There literally isn't a single good reason for spaces other than wanting to force ones own formatting preferences on others. Spaces are harmful, using them is dumb, defending them is toxic.

Collapse
 
cloutierjo profile image
cloutierjo

I actually arrived to the same conclusion after reading summaries of both sides arguments a few years ago, their is no valid argument to use space!

Collapse
 
ervin_szilagyi profile image
Ervin Szilagyi

TL;DR: Don't use Tabs. It is not a "personal style decision"

With all the respect, but I strongly disagree with this, at least with the first part. I agree that it should be a personal decision, but stating that you should use spaces can be blatantly ignorant towards people who have accessibility needs. I suggest reading this post: reddit.com/r/javascript/comments/c...

Collapse
 
mcsee profile image
Maxi Contieri

interesting post. thanks for mentioning

Collapse
 
yoursunny profile image
Junxiao Shi

go fmt enforces TAB and you shouldn't fight against the official code style.