Justin's Blog

Tabs or Spaces

The programmer's dilema; a problem so old and clichéd that just the question sparks controversy in every style guide, Git issue, or forum post. The contenders are:

Spaces

Spaces are, at least in most fonts used by programmers, fixed-width. They are the exact same size as any other character, and so you can guarentee that anybody who opens your code (in an editor that doesn't automatically reformat everything) will see exactly what you see.

The issue? Not everybody will like what you use. While you might enjoy your eight spaces, somebody else will think it's a horrific crime against their eyes. Now if they want to contribute to your project, they either have to endure your terrible taste in visual formatting, or deal with constantly converting between two indentation styles. And that isn't even the worst part:

The Worst Part

Joking aside, not everybody has a choice in how to read your code. Those with visual impairments might have to use a larger font and prefer a super low tab-width, and now your choice of spacing forces them to struggle to even see your code. This becomes more of an issue than that of preference; should you make it easier for others to read your code how they like? Or should you force your styling upon them?

Tabs

Tabs are represented by a single character code. This allows each user to define for themselves how much whitespace they want. The caveat, however, is that you can't use tabs when aligning specific text elements, because each person will choose for themselves the width of the tabs.

My Solution

So here's my brilliant and totally original1 solution to this problem that everyone should adopt immediately: use tabs when indenting code into groups, use spaces when aligning things character-wise. I'll show below, using |...for tabs and + for alignment spaces.

/* with spacing characters */
int int_pow(int x, int pow){
|...int start,
|...++++++res = 1;

|...for(start = 0; start < pow; ++start){
|...|...res *= x;
|...|.../* formatted
|...|...+++multiline
|...|...+++comment */
|...}
}

/* spacing characters removed, tab-width=4 */
int int_pow(int x, int pow){
    int start,
          res = 1;

    for(start = 0; start < pow; ++start){
        res *= x;
        /* formatted
           multiline
           comment */
    }    
}

/* spacing characters removed, tab-width=2 */
int int_pow(int x, int pow){
  int start,
        res = 1;

  for(start = 0; start < pow; ++start){
    res *= x;
    /* formatted
       multiline
       comment */
  }    
}

/* spacing characters removed, tab-width=8 */
int int_pow(int x, int pow){
        int start,
              res = 1;

        for(start = 0; start < pow; ++start){
                res *= x;
                /* formatted
                   multiline
                   comment */
        }    
}

See, now everybody can see your "super important" code alignment while also having their own indentation preferences. Win win.


  1. Somehow my original ideas and opinions ended up on this website that I found while checking if anybody else had this opinion, which instead of using as a sign that I shouldn't post what other people have already said I will use as a source to prove how correct I am.↩