Skip to content
loading...

Tabs VS Space

twitter logo ・1 min read  

tabs-space-war

Hello everyone !

I was asking myself this morning what are the diffs pros and cons between the use of Space and Tabs in code.

Also what do you use and why ?

Personally I use Tabs because I've never felt the need to change the default config of my IDE/TextEditor and my company doesn't force us to use one or the other (yet!)

Happy Coding!

Update : Thank you all for your answers ! According to your answsers : Consistency is the key :)

twitter logo DISCUSS (29)
markdown guide
 
 

Can't stop laughing everytime I rewatch this, LOL :))

I'd say, it's just a movie, but my geek part in my just can't stop yelling:

  • You can use soft tab!!!!!!!
  • Vim FTW!!!!!!!!!
 

Honestly though, the tabs vs space discussion is all about soft vs hard tabs. I don't know anyone who actually presses the space key manually several times and whilst they probably exist they are likely a really small minority.

 
 
 

I use a soft tab setting 4 spaces. I like a spacious feeling

 

I like 2 spaces, because I dislike wrapped lines and I hate horizontal scrolling, and a 2-space indent (even a few levels deep) leaves more room for a reasonably-long line, while still being visually distinctive enough to make the indentation useful.

But this is, like, literally the least important thing I can think of. I will do 6 spaces if that's the code style of the project I'm working on. Consistency is more important to me than personal druthers.

 

Just want to say that automatic readers that blind people use, take more time reading the spaces than the Tabs.

A blind developer friend of mine told me he prefer tabs for that, and just for that is enough for me.

Regards.

 

Personally I use 2 or 4 spaces (soft tab) depending on the project.

But I grew to love editorconfig.org/ lately so I don't really mind much either way. I just commit a .editorconfig file and the editor takes care that I follow the style of the project and that everything is consistent.

 

I/we use soft tab represented as two spaces and our linter tells us if we're wrong. If I worked in a tab-driven language I'd use tabs.

By the way, this works way better as a discussion like this than a poll, because it's not always clear what people mean by the two answers. The poll and answers are always hilarious though.

 
 

We need to make formatting simpler:

  1. Adopt a coding style…
  2. Automate it.
  3. Never think about it again!

In fairness, step 1 is the hardest. But the reward of step 3 far outweighs the pain.

 

Like how Go has gofmt. It's all opinionated in ways I don't like, but I guess you get used to it and at least you don't have to think about it.

 

Exactly. You're seeing this more and more in modern languages like Go, Python, etc. Formatting is just built-in to the language.

It takes some getting used to. But only because formatting has become such a personal thing.

 

I use soft tab, setting to 2 spaces, and it's great! working on small screen with big font size (13px), 2 spaces is enough.

But sometime it's conflict with team's coding convention, I solved it with editorconfig files :D

 

I'm mostly coding Python, I'll let you guess why I'm pro spaces :)
(and ctrl + maj + P > spaces > Indentation: convert to Spaces works great on Sublime Text)

It also allows me to align some of my parameters on big declarations without having a messy display.

 
 
 

No needs, you can use tabs and then the shortcut or set tab spacing at the bottom right of the editor

 

Tabs are the correct choice. I've covered this in my article, Lingering Spaces and the Problems of Overformatting

The key points are:

  • Tabs offer coders flexibility in how their code appears to them. Indentation can play a role in personal productivity. Tabs work well with proportionally spaced fonts.
  • Spaces encourage a lot bad code style where people line up symbols in random places on the line, which makes reading more difficult and can mess up version control.

Projects that use spaces inevitably make it harder for some coders to contribute to their project. And if they fall victim to the second point they make maintenance as a whole harder.

 

My big concern has always been what the Tabs Are Evil article calls "continuation lines". I think code like this (from that article) is ugly and hard to read.

int f(int x,
      int y) {
    return g(x,
             y);
}

Instead, why not structure it like this?

int f(
    int x,
    int y
) {
    return g(
        x,
        y
    );
}

Isn't that much easier to read? With indentation levels you know exactly what everything is.

 

Yes, exactly. If you wish to align things just start them all on the next line.

The space approach is entirely annoying, not because they're spaces, but because all functions have different alignments.

int go( int f,
        int y );

int somewhatLongerName( int f,
                        int y );

This sucks for readability, but also has a very real cost in maintenance and source control. If you wish to rename a function you have to modify the alignment of all the parameters again, this needless increases the patch size, thus wastes reviewer capacity.

 

I've always preferred tabs since I like two character indentation but most people on my team use 4. The argument against tabs from what I've read is mainly coming from alignment (things like aligning variables), which I find hurts readability.

 

Happily I'm using more Go now, and this fight was decided by the go creators, no more wories for me :))

Also in other languages I use auto-format on save so ... I really don't waste time on pressing tabs or spaces, or semi colons, and {} , that's the job of the IDE.

You will find pros vs cons on the battlefield (internet) but I think most of them are stupid. Because of we can choose our own local font and other IDE settings, you can make tab looks like spaces and viceversa anway.

 

I use spaces because tabs make git diff show misalignment that isn't there.

 

i like tabs, because it's easier to align. at work our overlords demand spaces, so every time i have to go backspace-backspace-backspace-backspace-backspace-backspace-backspace-backspace-backspace-backspace-backspace-backspace-backspace-backspace-backspace-backspace-backspace-space i swear a little.

there are benefits to spaces:

  • copy and paste into documents/chats/email/something has the same formatting everytime
  • it looks the same in every dev environment

tabs have some pros too:

  • it's better than using spaces to indent
 
 

This totally depends on the language you're coding in. For example python's PEP8 suggests using space instead of tabs (with 1 unit of indentation equaling 4 spaces). While in contrast, Go advocates use of tabs, in fact that is the only way that Go recommends. They even wrote an official tool called 'gofmt' which formats your code to the required standards (tabs, hanging opening braces etc.) so that everybody's code is following the same standards and we can end the styling debate once and for all(at least in the Go world).

Classic DEV Post from Apr 12 '18

Docker and NodeJS: Getting Started

Let's Dockerize a simple NodeJS application

Chardenal Matthieu profile image