Since everyone just has to weigh in on this…
There are several issues that have been raised in the comments here that I feel like addressing.
One is the amount to indent, which isn’t really about tabs vs. spaces, except people have said, more-or-less, if you use tabs you can see it whichever way you want.
Good old KR C promoted indenting by 1 tab, where a tab is equivalent to (up to) 8 spaces. That’s too wide! you say, What if I need to print it? you say. The argument was that if it was too wide to print – that is, indented too far – then your code needed restructuring anyway. You were doing too much in-line to end up with an indent that wide and you should refactor some of that into functions.
Now indents of 4 are so common now that I stick with that myself, but I’d still apply the argument to people who say they need an indent of 2. If things are getting too wide with an indent of 4 so you need to use 2 then something is more fundamentally wrong with your code than the way you use whitespace.
This easily leads to the tabs vs. spaces debate; again people have said you can see it how you personally prefer it if you use tabs – see it as 2 or 4 or 13 if you like. Except – I use multiple tools on a source code file; the IDE, sometimes a plain editor, VIM, other non-editing tools. Somewhere along the way, like, maybe =printing=, WILL treat your tabs as 8 space tabstops, and now your code is indented deeper than you thought. Also, somewhere along the way, someone will have indented or added extra indentation using spaces (as Harvey pointed out). Those don’t line up anymore because they assume a tab stop of X (where X 8)
My take is – it doesn’t matter what you want tabs to be, they are 8.
Even if you make your whole system consistent so that tabs always cause an indent of 4 (or 2 or 13), some fool will open your file on their system and all bets will be off. The only 100% safe way is to use spaces
BTW, what some people were wishing for – have the editor just present it the way you like it, even maybe including variable and function names – is called Source Code In Database (SCID) and there have been / are systems that do it.
So it comes down to, for me
- indent levels are 4
- tabs are 8
- pressing the tab key gives you 1 indent level
- which is 4 spaces (never tabs)
No extra keystrokes just because I’m using spaces. Shift Indent Level left or right commands are in the IDE/editor so no crying about I have to delete N spaces instead of just one tab
Indenting is always the same no matter who’s looking at it on what machine using which tool.
That’s what I do, with the reasoning behind it.
Stephen P.
(and Adam V – not picking or anything, but do you also trim trailing whitespace and use only Unix style LF, not CRLF pairs, for line endings, all to save disk space? I personally loathe the way IDEs leave trailing whitespace on lines and leave lines that include whitespace-only because you hit enter-enter to get a blank line. Doesn’t matter much for code or plentiful disk space but when you end up with HTML that’s 30% bigger due to that crud it does affect your bandwidth)