Phil CK

Tab Size

Last updated on

Code formatting is usually a conversation I absolutly hate with a passion. The hours wasted usually because some senior programmer defines his status by telling people how to code. Code consistancy is highly highly overrated, I have never heard anybody ever say “oh man I wish STL capitalized types”, or “I wish OpenGL used undescores”. We are happy to use 10 different 3rd parties written in 10 different styles and nobody ever complains. However my stance of tab lengths has changed, because I can see a messurable benifit.

Most commercial code I’ve worked on has been two spaced tabs like below, some of that code had no limit on line length some had 80 char limit.

void
some_func() {
  if(a) {
    int *b = get_int();
    if(b) {
      do_something();
      int *c = get_int();

      if(c) {
        do_something_else();
      }
    }
  }
}

I tried out 8 space tabs because the Linux kernel does it and I thought it was mad. However I am converted. Not only is it so easy to read at a glance, if you are using a 80 char line limit, you have to refactor often, just to reclaim line length.

The result straight to the point code, in small functions. Woo

void
some_func() {
        if(a) {
                int *b = get_int();
                if(b) {
                        do_something();
                        int *c = get_int();

                        if(c) {
                                do_something_else();
                        }
                }
        }
}

also because line length is valuable real-estate you tend to favour early returns.

void
some_func() {
        if(!a) {
                return;
        }
        
        int *b = get_int();
        if(!b) {
                return;
        }

        do_something();
        int *c = get_int();

        if(!c) {
                return;
        }

        do_something_else();
}