Source Code Indentation

I hate using spaces for indentation. I’m just putting it out there. My work mates think it’s a bit silly to have such a strong opinion on such a minor (mostly invisible) thing, however the pedantic perfectionist in me feels strongly about this.

Why do I hate using spaces for indentation? I’ll let the comparative advantages (and disadvantages) speak for themselves.

Spaces

  • Dumb text editors  that don’t allow you to control tab size and show it as a massive amount of space (*cough* notepad)  don’t show the code up nicely since you mostly want a smaller tab size for readability.
  • Further to the last one, Unix consoles don’t give you control over tab size and thus your code might not fit into the console screen whereas it might if you chose how many spaces you wanted.
  • Spaces mean that sequential related lines of code can be indented at the appropriate relative level no matter what the tab size (see Smart Tabs).

Tabs

  • Everyone can control how “big” your indentation appears to them if they are using a text editor that gives then that ability (most do), if they like massive indentation then they can make it 8 spaces, vice versa they can make it 2 spaces worth if they like that (I prefer 4).
  • Further to the last point, if you have a vision impairment that means you need to have a large font size then you can decrease the size of the indentation so more code fits on the screen.
  • When de-indenting code you can press backspace rather than shift-tab (assuming that your editor supports shift-tab, if not you have to press backspace multiple times). Some editors are smart enough to remove all the indentation when pressing backspace and you are using spaces, but not many.
  • Any parsing you want to do that needs to figure out indentation becomes easy, you don’t need to try and figure out how many spaces equals one indentation, it’s a tab character.
  • Smaller file size (not a huge advantage)
  • Semantically, a tab means “provide some indentation”, spaces mean “provide some white space between words” – a tab character is the better semantic choice.

Conclusion

As far as I’m concerned, tabs are better in every way, the first two “advantages” of spaces are for people working in archaic environments and don’t know what they are doing (this won’t be the case 100% of the time, but if they know what they are doing then they will be able to either put up with it or use a better environment). The last one can be fixed by either using the combination of tabs and spaces mentioned in the Smart Tabs article (kinda gross) or doing what I do and simply indent the consecutive lines of code a single indentation level rather than trying to match them to some arbitrary point on the previous line (which I think looks ugly).

Addendum

In saying this, where there is a clear convention for a programming language that most people use that involves using spaces for indentation I will usually try and follow that because it helps with reusing and contributing code.

One thing I hate more than using spaces for indentation is inconsistent indentation (I also dislike commits that involve changes in indentation mixed with code changes). Consistency of coding conventions is very important – it improves code readability and maintainability (and in terms of indentation saves from painful commit diffs).

By the way, at the end of the day, if you are using a good IDE then there is little difference between tabs and spaces.

Further Reading

5 thoughts on “Source Code Indentation”

  1. This advantage of Spaces is a reason why one might use spaces with Visual Studio: “Dumb text editors that don’t allow you to control tab size and show it as a massive amount of space”

    In VS, when you move the cursor near a brace it highlights the opposite brace. If that brace is offscreen if shows a tooltip showing the context of the brace that is off-screen (very handy). Unfortunately it renders tabs as 8 spaces.

    In fact VS doesn’t always respect that you have chosen tabs, so it occasionally inserts spaces instead of tabs. I don’t really want to call VS a ‘dumb text editor’ but it really really expects you to use spaces.

    Given that we all have inconsistent indentation more than anything else, I feel we must switch back to spaces… or change text editor ;)

  2. Yep, me writing this article was a precursor for moving back to spaces.

    After writing this, using tabs with .NET / VS is untenable :P

  3. I don’t think SmartTabs will work the way it’s intendend to or give you the result spaces will. For example R# and some people will put the brace under the L of List
    var xxxx = new List
    {

    This is common with lamdas, object initializers and other blocks. However, SmartTabs won’t really fix this, since the intention of the dev was to align it under L, but if someone has a different setting for tabs (e.g. the original dev used 4 and another dev uses 2) the { will appear under x.

    BTW, I normally just align it at the beginning of the prev line (in this case under the v or var), but there’re certain instances where I need to align something

    When you talk about semantics of indentations, you’re missing an important bit, isn’t only about indentation, but also about alignment. Spaces provide a better control for alignment and work for indentation as well. Different settings for tabs, which seems to be the main advantage of them according to what you mentioned actually breaks alignment.

    That said, I don’t really care about all this. I’ll just happily ignore this setting and go with the power of defaults.

    1. But with smart tabs the line before will start with tabs too so when changing from a size of 4 to a size of 2 the previous line will shrink and the brace will still line up at the same place.

      I’ve never see a need to do that alignment thing, as I said I think it looks ugly and actually makes code harder to read.

      Spaces do provide a better alignment control (hence the invention of the smart tabs thing). However, as I said I don’t think that alignment should be used in that way, I think that only indentation should be used. Some might think that naive, but in my experience I’ve never seen a need for it.

  4. My workmates pointed out that this post seems to really seriously vindicate the use of tabs above everything else. I should probably correct this:

    This was a satirical play on my part. I do feel strongly about using tabs, but I have a much stronger feeling that consistency is very important.

    As I said at the end, I will happily deal with spaces if it’s the de-facto (or otherwise) standard, and if for some reason alignment is important then I can deal with just spaces rather than the inconsistent bastardisation that is SmartTabs.

    Happy Royce ;)?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>