Saturday, April 12, 2008

To Tab or Not To Tab

Modern code editors format code automatically, providing indenting so that looping and other control logic is visually apparent. Indenting can also be added manually, normally using the tab key. Different developers have different preferences for how much to indent each time, so they configure their editors as to how much space a tab character should take on the screen, typically between 2 and 8 characters wide. Different developers also have different preferences as to whether the tab should remain a tab character, or whether it should be converted and saved as some number of space characters.

On a shared project it is important to agree on a convention. If some lines use tabs for formatting, and others use spaces, the lines get out of synch whenever two developers have different tab widths. The code looks jagged and becomes difficult to read.

There are essentially two conventions. One has all tabs converted to spaces at an agreed width, the other keeps tabs always. This choice often turns into a religious argument.

Both conventions work if followed by all group members. Both can fail if a heretic is present. But one convention has a distinct advantage. It allows different developers to use different tab sizes to suit their preferences without affecting other developers. This is the better convention. I'll leave it to you to figure out which one this is.