Page info
Author
Wichert Akkerman
Navigation
Home
Where it all starts
Code
Useful and useless code
Presentations
What I have been telling others
Rants
I love to complain
My sites
Paradiso calendar
An easy to use calendar
HugeURL
For better URL bragging rights

Tabs versus spaces

Coding style is always a topic for hot debates, and there are different standard guidelines you can follow such as the GNU Coding Standards ,the Linux kernel coding style and pythons Style Guide for C Code and Style Guide for Python Code (which has the same authors but is quite different amusingly enough). Coding style consists of many different aspects such as naming conventions, whitespace in commands and how to use parenthesis. This rant only focuses on indenting though.

Indenting is designed to make code more readable by reflecting the control flow in the leading whitespace. This is generally a good idea since it makes it a lot easier to quickly see how code is structured. In fact this idea is so good that python makes it mandatory by making the indenting a part of its syntax: instead of using things like braces ({ and }) or keywords (like begin and end) it uses indents to indicate the start and end of a block.

Now we get to the point that is somewhat controversial: how much indenting should be used? 2 spaces? 4 spaces? 8 spaces? And should we use spaces, tabs, or a mixture of them? The way I see it there are three important factors in making that decision:

  1. Indent width (or shiftwidth in vi-speak) is a highly personal thing. Some people prefer large indents to make control flow more obvious and force people to not use too many nested constructs (since that would make things go off the right side of the screen). Others have a more compact coding style and use four or even two spaces.
  2. It is very important that the indents used throught a project are very consistent. There are few things more annoying than seeing different coding styles used in different files (or sometimes even inside the same file!).
  3. Tab sizes are configurable in all editors (except for really simple ones which you do not want to use when coding anyway). With good editors you can even configure the tab size per filetype or per file

These three factors lead to an obvious solution: always use a single tab for indenting. This solves the both problem of keeping things consistent (use tabs everywhere) as well as the problem of differing indent width preferences (everyone can change the tab width in their editor to their preferred indent width).