Don't use tab characters

Most code editors allow you to use TAB characters in your source code. Such TAB characters will appear at the start of each line of your code, controlling indentation. The problem is that TAB characters are inherently ambiguous, and different tools can render TAB characters in different ways. What may look reasonable to the author, at time of writing in their particular environment, may be rendered in a significantly different way in some other tool.

For example, say the following code appears in your editor as:


import java.util.*;

public final class TabsOne {

  void decideWhatToDo(){
    if(! bored){
      continueWhatYourAreDoing();
    }
    else {
      if (! hasCarpalTunnelIssues()) {
        code();
      }
      else {
        eatSomething();
      }
    }
  }

} 

If you are using TAB characters, then this could easily appear in another tool as:

import java.util.*;

public final class TabsTwo {

    void decideWhatToDo(){
            if(!bored){
                    continueWhatYourAreDoing();
            }
            else {
                    if (! hasCarpalTunnelIssues()) {
                            code();
                    }
                    else {
                            eatSomething();
                    }
            }
    }
} 

Even worse, some tools can even render text with TABs with negative indentation:


import java.util.*;

public final class TabsThree {

    void decideWhatToDo(){
      if(!bored){
    continueWhatYourAreDoing();
      }
      else {
        if (! hasCarpalTunnelIssues()) {
      code();
        }
        else {
      eatSomething();
        }
      }
    }
} 

Most people would consider such code as much more difficult to read, and annoying. Thus, using TAB characters in source code is often considered highly undesirable.

You should use 2-4 spaces to indent your code, not TAB characters. Modern IDE's let you map TAB keystrokes to a given number of spaces instead.

Here's a related quote from Code Complete, by Steve McConnell:

"The study concluded that two-to-four-space indentation was optimal. Interestingly, many subjects in the experiment felt that the six-space indentation was easier to use than the smaller indentations, even though their scores were lower. That's probably because six spaces looks pleasing. But regardless of how pretty it looks, six-space indentation turns out to be less readable. This is an example of a collision between aesthetic appeal and readability."

Would you use this technique?
Yes   No   Undecided   
© 2014 Hirondelle Systems | Source Code | Contact | License | RSS
Individual code snippets can be used under this BSD license - Last updated on September 21, 2013.
Over 2,000,000 unique IPs last year - Built with WEB4J.
- In Memoriam : Bill Dirani -