Ask HN: When you code, do you use tabs or spaces?

7 points by meerita ↗ HN
I've been reading many many internet jokes about the war on tabs or spaces but I never heard any single explanation why one is better than the other from any serious programmer.

I'm not a coder, I'm more designer/UX coder which loves to code. When I started frontend coding I always used tab spacing and view hidden chars and that was the way to do it in the past if you wanted to do clean HTML/CSS code. But since the first release of Textmate and Ruby on Rails, I've been instructed by seniors coders to use spaces and I never asked why. When I raised the question all I've got was some digressive rant.

These are my questions:

1. Your preference and why. 2. What do you think it is the correct one. 3. In your company did they tell you what to use? is there any coding guideline?

Thanks for sharing.

22 comments

[ 2.2 ms ] story [ 210 ms ] thread
1. I strongly prefer tabs, mostly because a lot easier to count than spaces, and because I do not like ambiguity (see 2).

2. I think both are correct. Arguably, tab is the indentation character whereas space is the separator character; but languages are human-defined, so I do not mind if people choose spaces over tabs.

3. Never seen company-wide coding guidelines. I have seen project-wide guidelines, which I think makes perfect sense. I believe mixing tabs and spaces for indentation in a single project, let alone in a single file, is extremely bad practice.

EDIT: as an explanation, this is because I regard tabs as individual characters; I believe the real problem is that IDEs will insert 4 (or sometimes 8) spaces when you type a tab.

I always use spaces because there is no ambiguity of interpretation (ie. 1 space is always 1 space whereas tabs can be set to different indentations depending on the editor). Using tabs leads to the potential for different indentation behavior with different editors and mis-formatted code.
Interesting. I use tabs because there is no ambiguity. One tab is one level of indentation. How big that indentation appears on screen is up to the viewer.
Spaces.

Using spaces practically guarantees that code will look exactly the same for everyone, and that people won't accidentally exceed the project's 80-column rule because of how they've set tabs in their editor. It doesn't take too much diligence to avoid these issues when using tabs, but it's more than zero.

It's not a company rule; it's a project rule. I think using tabs is very slightly less optimal, but what's more important is that people be consistent across the project.

Tabs for leading indentation, space for alignment would be my favorite combination. However, in practice, it seems too complex to enforce. So space everywhere is good enough.
I use spaces. As notacoward mentioned, spaces guarantee that code will look consistent across environment. Spaces are easy to set up and there's no dealing with different tab sizing or other similar issue. But, in the end, consistency is what really matters. A fully tabbed project is better than one that mostly uses spaces.
Tabs purely because there are fewer characters to type compared to spaces. But for consistency's sake as mentioned above, I set expandtab in vim to the required number of spaces.
It depends on what environment you program in. I do most of my work using MATLAB or Xcode, and they both convert tabs to spaces. I know spaces are the correct way to do things, but it seems like most modern development environments take care of that for you.
With only tabs you can't align code like this:

    name_of_func_here(foobar_arg1,
                      foobar_arg2,
                      foobar_arg3);
You can if you mix tabs and spaces but then you run into other awful issues because different editors convert tabs to different number of spaces.
Make requires tabs (at least a leading tab).

Anything but an all-spaces practice will make you miserable in Python.

But in practice, in any environment where I've worked, there's never been a problem with mixed tabs and spaces for most languages. I've only ever seen this debate online, never in the office.

Tabs align at boundaries of 8 spaces by default in all environments in which I've worked. (Tab characters, that is. I'm not talking about what an editor might insert when you hit the tab key.) Are there any significant environments where this is not true?

Tabs work fine in Python if you don't mix them with spaces. Or is there a problem with tabs I've never encountered?
True. But my point, really, was that in most circumstances even tabs mixed with spaces isn't so very bad. I mentioned Make and Python as examples of exceptions.

BTW, do you really use tabs in Python code? I've not ever encountered that.

I use tabs wherever possible, yes. Though that might change in the future. I'm not sure why spaces are so common in the Python world.
I use the tab button, but what actually goes into the file is up to Sublime :)
Tabs. But I thought about changing because apparently over 90% of Python developers on GitHub use Spaces.

Problem is, for every benefit there is a downside and some of the benefits aren't that interesting to me.

Benefits seem to be:

1. Alignment without mixing (which is bad in some languages). I don't align.

2. Code looks the same for everyone. Doesn't need some workarounds if you publish code or present it outside the editor to get your prefered width.

Downside is:

1. I use variable width for some filetypes. People might not agree with that. I wanted to convert all my tabs to 4 spaces when I realized that I've set sass to 2 and sometimes change it to 4. Conversion is possible, but tricky especially if you align.

2. OCD/Perfection clashes. Tabs kind of make sense for indentation.

The problems I have with tabs are:

1. Barely anyone seems to use them anymore.

2. Some languages I like (like YAML) don't allow tabs.

3. Needs some non-standard CSS to get them to display as 4 Spaces in the Browser instead of the overly wide 8. I think GitHub converts them automatically to spaces in the preview to avoid that issue (though your still at 8 then).

I guess I might end up with spaces in the near future. It's kind of a "What I like" vs. "What others use" situation. I don't think there is anything wrong with tabs unless you really have to align your code.

Tabs.

The argument that spaces are better because they make the code appear the same to everyone is irrational. Some prefer 2 space indentation, some 4, some 8. You can set that to your own preference with tabs. Not with spaces.

Also, using spaces feels counterintuitive. It hurts my brain when I see that there are four characters expressing what could be done with one.

The problem is that "setting that to your own preference" always leads to code that fits on your screen nicely but wraps on someone else's, or that's aligned for you but not for others, and so on. In any multi-person project it's just a latent source of irritation and conflict. Why bother, when any modern editor can DTRT by letting you add or remove indentation with a single key even though it's multiple spaces?
Spaces.

First of all, I like Python-like syntax (Python, Haskell) and tabs mess code very easily.

Tabs are:

1) of a different length in different environments and you can't control all of latter (skype, github, web, terminals, other text editors).

2) an archaic character originally designed for typewriters and re-purposed then into column alignment symbol for terminals (I think that tabs are good for formatting table columns in text mode) and then into indentation symbol for some reason unknown. I think if we want formatting, some more modern tools should be used. Why don't we use vertical tabulation and other weird control symbols from 70s?

2a) not minimalist. I can live without them (whereas I can't give up spaces), so why would I need it? I want plain text to be real plain text, without some formatting options living on their own.

3) my tools (square selection, smart indentation in vim) make work with spaces easy, they type spaces for me. When I'm forced to work with tabs, I also have to make additional explicit checks before every commit to make sure I haven't messed up anything.

4) I can adjust to an existing code style (thus far a style is good and consistent: 2/4/8 spaces is ok for me as far as that style is consistent), I don't have an urge to make it look like my own code every time.

tl;dr: don't try to make the world look as you want with tabs, adjust yourself to spaces!

Python dev here. If I had my way, I would use tabs for indentation and spaces for everything else. However, the general convention now is to use 2-4 spaces for indentation, and that's what I'm using.
tabs for indentation, spaces for alignment. We all prefer different indentation sizes, and this technique takes care of that issue. If you want an indentation size of 2 [spaces], go for it, if you want 8, no problem.
I prefer using the spaces but sometimes tabs comes habitually while coding so i an used to configure my editors to treat the tab as 3-4 spaces based on programming languages. The only downside is when you try to use backspace you have to use it 3-4 times rather than 1 in case of tabs.