Ask HN: When you code, do you use tabs or spaces?
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 ] thread2. 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.
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.
https://github.com/jquery/jquery/blob/master/src/intro.js
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?
BTW, do you really use tabs in Python code? I've not ever encountered that.
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.
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.
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!