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