Skip to main content

Command Palette

Search for a command to run...

Code Smell 164 - Mixed Indentations

Tabs vs Spaces. The most significant computer problem

Updated
2 min read
Code Smell 164 - Mixed Indentations
M

I’m a senior software engineer loving clean code, and declarative designs. S.O.L.I.D. and agile methodologies fan.

TL;DR: Don't mix indentation styles

Problems

  • Readability

  • Code consistency

  • Standards violation

Solutions

  1. Choose one of them

  2. Stick to it

  3. Enforce it with code standards tests

  4. Share the rules on all the codebase

  5. Use an IDE like VS Code or WebStorm that doesn't include tabs at all.

Context

Whenever I publish an article many people don't care about the sample intent and rush to point at indentation mistakes.

Choosing one standard over the other will be a great solution.

Spaces always count as one.

Tabs can count as many different options.

Sample Code

Wrong

function add(x, y) {
// --->..return x + y;

      return x + y;
}

function main() {
// --->var x = 5,
// --->....y = 7;

    var x = 5,
        y = 7;
}

Right

function add(x, y) {
// --->return x + y;
    return x + y;
}

Detection

[X] Automatic

Any parser can enforce this rule.

Exceptions

Some languages like Python consider indent as part of the syntax.

In these languages, indentation is not accidental since it changes code semantics.

Tags

  • Code Standards

Conclusion

There's been so much debate on this subject.

The smell is related to mixing them, not about using one instead of another.

Some IDEs automatically convert one convention to the other one.

Relations

More Info

Disclaimer

Code Smells are just my opinion.


indentation joke

Whatever the device you use for getting your information out, it should be the same information.

Tim Berners-Lee


This article is part of the CodeSmell Series.

Code Smells

Part 1 of 50

In this series, we will see several symptoms and situations that make us doubt the quality of our developments. We will present possible solutions. Most are just clues. They are no hard rules.

Code Smell 164 - Mixed Indentations