Tabs or spaces, are equivalent?
TL;DR: Don't use Tabs. It is not a "personal style decision"
Problems
Readability
Compilation errors in some languages
Solutions
Use spaces. Always.
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))
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))
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
Code Smell 164 - Mixed Indentations
Maxi Contieri ・ Sep 21 '22
Code Smell 48 - Code Without Standards
Maxi Contieri ・ Dec 10 '20
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
Software Engineering Great Quotes
Maxi Contieri ・ Dec 28 '20
This article is part of the CodeSmell Series.
Top comments (12)
How to start a flame war labeled as code smell.
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.
No flame war. Just polite arguments
Counterpoint:
Why we should default to Tabs instead of Spaces for an 'accessible first' environment
Alexander Sandberg ・ Jul 12 '19 ・ 3 min read
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.
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.
Accessibility is a very good point
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.
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!
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...
interesting post. thanks for mentioning
go fmt
enforces TAB and you shouldn't fight against the official code style.