1) no TABs, only SPACEs.
Pros: code layout does not depend on a chosen TAB size.
Cons: it is easy to screw proper indentation with a BACKSPACE; it is annoying to hit BACKSPACE 4 times instead of once (given tabsize = 4)
2) TABs are everywhere.
Pros: easy and strict; you cannot screw indentation because it is always aligned to a grid
Cons: changing tab size may produce weird result when TABs are used for alignment in a middle of a line
3) random mix of SPACEs and TABs.
Pros: none.
Cons: this approach is a pure evil.
4) TABS are for indentation level only, SPACEs are for alignment.
Pros: same as in styles 1 and 2 -- no dependence on tab size, easy, strict.
Cons: unfortunately, code editors usually supports either style 1, or style 2, so you cannot just use TAB key everywhere and expect that your editor will be smart enought to replace TABs to SPACEs in proper places. At least, in VisualStudio it is so. May be other code editors are smarter?
I personally prefer TABS over SPACES, and, when coding alone, I just use the style 2. The "tab size problem" does not bother me because I just never use garbage editors/viewer which are not capable to configure the tab size.
In a team, I have to adapt to those members who still use the shitty Notepad and suffer from the "tab size problem". On the other hand, I don't want to lose all benefits of TABs, so I'd prefer to use style 4: TABs for indentation, SPACEs for mid-line alignments. Code editors are the only stoppers here.
So, what can we do at least for a VisualStudio Code Editor? Fortunately, there is a trick: you can use a combination of three commands:
>Edit.SelectAll
>Edit.ConvertTabsToSpaces
>Edit.TabifySelectedLines
So, you can use TABs for editing everywhere, and then, before committing the file, just run there 3 command in the VS Command Window. Command names are almost self-describing:1.
Edit.SelectAll selects the whole code text2.
Edit.ConvertTabsToSpaces converts all TABs to SPACEs3.
Edit.TabifySelectedLines converts SPACEs back to TABs, but works only on leading SPACEs! That's the trick.Unfortunately, the solution is not ideal: it replaces all leading SPACEs to TABs, though there are cases when you should not do it. For example, if you break a long function parameter list into several lines, you're actually staying on the same indentation level and want the parameter list be adjusted, not indented. Improving the algorithm will require source code parsing to find cases when a new line does not introduce a new indentation level.
Anyway, we still have a quite decent solution. But typing these commands again and again is unproductive. If you find yourself repeating -- write a script. So it is better to translate these commands to a macro:
Public Module Editor
Sub TabifyAll()
Dim curPoint As VirtualPoint
curPoint = DTE.ActiveDocument.Selection.ActivePoint
DTE.ActiveDocument.Selection.SelectAll()
DTE.ExecuteCommand("Edit.ConvertTabsToSpaces")
DTE.ActiveDocument.Selection.Tabify()
DTE.ActiveDocument.Selection.MoveToPoint(curPoint)
End Sub
End Module
Now we can add the macro as a menu command, assign a keyboard shortcut for it, etc. I've put this command into "Edit" menu as a "&Tabify" menu item, so I can use "Alt-E,Alt-T" shortcut.
Also it is possible to write a similar macro which will "tabify" only a text selection instead of the whole file, but actually I don't think it makes a lot of sense: you usually want to keep style consistent over the whole document (and even over a whole project/solution/workspace), not just a small selection. I'd prefer a script which performs this action automatically upon a file save. May be someone will write a VS extension for this, someday...