Why should I use a tab width of four spaces?

There is only one reason I can think of:
var foo = 'duh',
    fee = 'der',
    fuu = 'dur';
You'll note this only applies to javascript ...


  1. Using tabs to align things in a programming language is almost always waste of time; it makes editing the code painful. There are rare exceptions, e.g. when there are symmetries in arithmetic expressions - think about matrix multiplication expanded out - but usually a waste of time.

    Indentation is all about making the syntactic structure code clear; nested productions are, as a rule, indented. Too large an indent, and deeply nested code starts to wrap or scroll, and look disjointed; too small an indent, and the structure is hidden. Max indent is thus limited by how nested the code is, which depends on the language and the complexity of the codebase. In practice, for most languages, any number between 2 and 8 is OK. Programmers like powers of two, so a good compromise is 4.

    Whether you indent with tabs or spaces is immaterial. Indenting with tabs makes it marginally easier for others to customize their editor to match their preferences, but they can always pipe through `unexpand -i -t 4 | expand -i -t 8` to convert from indent 4 to indent 8.

    Whatever you do, though, don't put tabs in the middle of lines to make things line up. Use spaces for that. And don't use that evil emacs tab autofill feature (e.g. with an indent of 4, it alternates between 4 spaces and tabs for 8 to "optimize" character usage). It's nasty.

  2. Good points all, thanks very much for a little more insight than hacks such as myself can give.