Coding Standards

about | archive


[ 2006-June-15 16:19 ]

I have recently started a new job at a company that has extensive coding standards. I think coding standards are a good thing. Consistent formatting and naming helps make code look more familiar and understandable. One new thing for me is that this company has somewhat draconian procedures in place to ensure that code follows the standards. There are even automated tools which check code for compliance with some of the guidelines. I think this is wonderful, as it makes it that much easier to comply with the requirements, and makes the code that much more consistent. Like everyone else, I have my preferences and prejudices about what good code looks like. However, my philosophy is that consistency with everyone else trumps my personal taste. That said, there are two common requirements I strongly dislike.

The first is the very common line length limit of 80 characters. This tradition dates back to the ages where everyone used text terminals. However, today every developer should have a reasonably high resolution screen which can display way more than 80 characters per line. I agree that extremely long lines are hard to read, and can be a sign of poor code. I understand that deeply nested and indented code probably needs to be broken out into its own function/method/whatever. However, I still think that 80 characters are not enough. My brain has been trained to think that one line one executable statement, and thus reading code that has wrapped is more difficult. I'm not sure that no limit at all is a good policy, but I think that a limit of 100 or 120 characters would be much more reasonable.

The second requirement I dislike is complex indentation requirements. Commonly, this requires wrapped function parameters to line up. I hate this for two reasons. First, it requires me to type a careful number of spaces, instead of just hitting tab once or twice. Some would argue that I need to use a better text editor, but I prefer the simpler editors, and it is much easier to just insert one tab instead of having to visually line up two elements. The second reason is that I am one of those rare programmers who likes to look at code in a variable width font. I typically use Helvetica, Arial, or Bitstream Vera Sans, depending on my platform. This means that everytime I need to line up parameters, I need to enable my editor's "use monospaced font" option, do the indentation, then turn it off. Very annoying.

In my ideal world, the normal indentation uses tab characters, the tabs are set to four spaces, and code that wraps over a line uses two tabs. Thus, this is my idea of perfection:

void MyCPlusPlusClass::someReallyLongFunctionName(Type1 ParameterOne,
        Type2 ParameterTwo, Type3 ParameterThree) {
    // ... code begins here ...
    Type someVar = someCode;
}