13 ms·
Ask HN: Code etiquette - tabs or spaces?
I like putting coding with tabs only but I've been told that using spaces is more uniform across various IDEs/what have you.
what do you use?
- simianstyle 18y agoMy theory is that your IDE should be able to adjust the amount of spaces that 1 tab is equal to, and therefore the whitespace is adjustable across editors that way - so I always use tabs. But if you're so anal about whitespace, there's always regex...
- newt0311 18y agowhitespace. Tabs are non-standard across systems and of a non-unit length and so when custom continuation lines are indented, they have to use a mix which wreaks havoc when the tab length goes from 4 to say 8 or vice versa. If you are interested in re-indenting code, then use a program/command that is actually designed for code re-indentation, not some inaccurate stupid shortcut like tab length manipulation. Pretty much any modern IDE would have a code re-indentation infrastructure.
- cperciva 18y agoTo quote BSD style(9): Indentation is an 8 character tab. If you have to wrap a long statement [because it would go past the 78th column], put the operator at the end of the line. Second level indents are four spaces. Do not add whitespace at the end of a line, and only use tabs followed by spaces to form the indentation. Do not use more spaces than a tab will produce and do not use spaces in front of tabs. If you find that your code doesn't fit into 78 columns using 8-character tabs... you need to refactor your code. If your editor mangles whitespace... get a better editor.
- ScottWhigham 18y agoWhy would someone downvote this? It's the guy/girl's opinion, it's about the topic, and I don't see any reason to downvote other than someone is just having a bad day or being pissy.
- lacker 18y agoA lot of decent code won't fit into 78 columns with 8-character tabs. An if statement inside a for loop inside a function inside a class is already 4 levels of indentation. Call a ten-letter function with three ten-letter arguments and you're over the limit. It depends a lot on the style of code... some apis will have really long names and require a lot of arguments. In this case you'll run over 80 columns even if you use two-space indentation (which we do at my workplace).
- ScottWhigham 18y agoPersonally I use whatever the editor that the team uses uses as the default (that's right "uses uses", baby! Don't get many opportunities for that!). I'm a big fan of using the defaults when it comes to development. I want to be able to step over to a co-worker's or temporary machine, do my work, check it in and not have to worry whether we used spaces or tabs.
- brianr 18y agoIf you're writing Python, the convention is spaces. From PEP 8: Indentation Use 4 spaces per indentation level. (see http://www.python.org/dev/peps/pep-0008/ http://www.python.org/dev/peps/pep-0008/) I'm partial to spaces in general. Most (all?) IDEs can insert spaces when you press the tab key, and doing so will prevent the nastiness that can happen when you mix spaces with tabs.
- wallflower 18y agoAll the companies I have worked at have enforced spaces by setting up the editor to translate tabs to 4 spaces. I never asked why. Some battles aren't worth fighting. 'Spaces instead of tabs' wasn't the near the near-religious fervour of which editor to use though (VSlick won at my 1st company - e.g. company bought group license - though you could buy your own fave). I guess if you spend all day working with a hammer, you want the best hammer for you. I've spent hundreds of my own to buy my own editor to use at work.