There is plenty of discussion on the web as whether you should be indenting with tabs or with spaces. But there's actually another answer: smart tabs. First, let's look at the advantages and disadvantages of both spaces and tabs, then we'll look at how smart tabs combines the benefits of each.
- Will always retain the same formatting, regardless of editor settings.
- You can use spaces to align items past the normal indentation on multiple lines (for those of use who like our equal signs or function parameters to line up)
- While most editors allow you to configure the appearance of a tab character to be 2, 3, 4, etc. spaces, when you indent with spaces it will always appear the same. So if I wrote my code with 4-space indentations, and you like 2-space indentations, you're going to be stuck with the original formatting
- Convenient (it's easy to hit tab). It's also a lot less use of arrow keys to navigate indentations
- Allows the user to configure the appearance of a tab character. So if you'd like indentation to appear as 2 spaces, but I prefer 4, all we need to do is configure our editor to display a tab in our preferred format.
- Using only tabs does not allow you to align things down to the character on multiple lines. You are stuck with the exact width of a tab
- Can lead to inconsistencies between the appearance in multiple editors. So if you have your tabs configured to 2 spaces, and I have them configured to 4, what appears to line up on multiple lines for you may not for me, and vice-versa.
Smarts tabs are a combination of tabs and spaces for indentations and formatting. A line formatted with smart tabs starts with the appropriate number of tabs to reach the current indentation level in your code. Then, any space beyond that initial indentation (in the interest of making things line up on multiple lines, etc) are done with spaces. This provides all of the pros of both tabs and spaces, and none of the cons.
- We can configure the width of the tab character to our preference, while working on the exact same code.
- We can align code on multiple lines.
- All the conveniences of tabs for base indentation.
Most good editors and IDE's support Smart Tabs these days. I'd highly recommend using them as your formatting standard.