DEV Community

Tabs VS Space

Chardenal Matthieu on January 10, 2018

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 ...
Collapse
 
johand profile image
Johan • Edited
Collapse
 
huytd profile image
Huy Tr. • Edited

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!!!!!!!!!
Collapse
 
imthedeveloper profile image
ImTheDeveloper

Silicon valley has been a great watch

Collapse
 
__shadz_ profile image
Chardenal Matthieu

I lol'd :D

Collapse
 
ardennl profile image
Arden de Raaij

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

Collapse
 
isaacdlyman profile image
Isaac Lyman

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.

Collapse
 
josheriff profile image
Jose

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.

Collapse
 
ben profile image
Ben Halpern

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.

Collapse
 
sir_wernich profile image
Wernich ️

two spaces? i want to fight you now.

Collapse
 
gonedark profile image
Jason McCreary • Edited

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.

Collapse
 
katylava profile image
katy lavallee

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.

Collapse
 
gonedark profile image
Jason McCreary • Edited

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.

Collapse
 
huytd profile image
Huy Tr.

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

Collapse
 
pbouillon profile image
Pierre Bouillon

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.

Collapse
 
roger profile image
Roger Stach

The repeated space sound bothers me though.

Collapse
 
roger profile image
Roger Stach

I still prefer tabs over spaces though. 😂

Collapse
 
pbouillon profile image
Pierre Bouillon

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

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

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.

Collapse
 
miffpengi profile image
Miff • Edited

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.

Collapse
 
mortoray profile image
edA‑qa mort‑ora‑y

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.

Collapse
 
bgadrian profile image
Adrian B.G.

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.

Collapse
 
chrisnelsonpe_10 profile image
Chris Nelson

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

Collapse
 
aghost7 profile image
Jonathan Boudreau

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.

Collapse
 
sir_wernich profile image
Wernich ️

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
Collapse
 
joshuakb2 profile image
Joshua Baker

Your editor doesn't do soft tabs, I guess?

Collapse
 
ishankhare07 profile image
Ishan Khare

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