80 Column Blues

There's generally a lot of disagreement about the best way to format code. One way to make every coding style unreadable is to enforce or require an 80 column/character line limit. A lot of coding standards still include an 80 character line length limit, including the Coding Conventions for the Java Programming Language. I've also seen 72 and 77 character line length limits in other coding standards. An 80 character line limit may have been a necessity in 1968, made good sense in 1978, was probably useful in 1988 and possibly helpful in 1998 but it's definitely annoying in 2008.

Left to my own devices I find that for block comments I write to about 100 characters per line which seems to be about the same line length as a typeset book, ie. a comfortable reading length. For code my average line length, not considering leading whitespace is probably 40 characters or less, again a comfortably short reading length. With whitespace my average line of code generally ends some where between column 65 and 75. Using inner classes and other constructs can result in deeply nested braces producing 60 or more characters of initial whitespace. This is where 80 character line limits really make thinks look ugly. I've seen code that has been "de-intended" (leading whitespace was removed) and still had less than 30 non-whitespace characters per line. The result was nearly unreadable.

In my own code I occasionally write lines with more than 80 non-whitespace characters. These are commonly logging, method calls, and statements using my friend, "?", the infamous "Riddler" conditional operator. Even though these statements are longer they generally aren't too difficult to parse and I don't find the extended line length a problem.

My current conclusions are:

  • Except for cro-magnon freaks we all have text editors that can easily edit text that's wider than 80 columns.
  • Forced line breaks to meet width requirements don't make code more readable. Forced line breaks often reduce readability.
  • 80 column hard limits should be dropped from modern coding standards
  • For block non-code text the width used should be a comfortable reading width. Really long lines of text are hard to read.
  • Consideration of maximum acceptable line length should ignore any leading whitespace. The intelligibility, or lack thereof, of a line of code is not strongly tied to the total line length but to the number of non-whitespace characters in the line.
  • The readability of a line of code is probably related to the density of the syntax. Lines using only simple syntax or syntax that can be easily decomposed can be longer and remain readable.
Comments:

While I agree that formatting some code - particularly methods with long and descriptive names - to 80 columns can decrease readability, there are a few aspects you have neglected to mention which argue in favor of a reduced column limit.

For one, if code is kept to approximately 80 columns it becomes very easy to work with two files side-by-side in the same editor (text or GUI), increasing the effective information density of the single display. Attempting this with higher column limits will generally result in a high degree of horizontal scrolling in order to follow the program flow whereas keeping to the 80 column limit allows this with little to no scrolling on most modern displays.

Another aspect is what happens when you transfer your code to a medium without the ability to scroll/nowrap, such as on paper. Keeping code to the limit keeps print layout sane. While this is much more rare event than editing code, staying within smaller limits satisfies both applications.

I also tend to be pragmatic about the application of the rule. I try to format my code to 80 columns except when doing so results in absurd levels of visual brokenness. Usually when this becomes a problem the code simply needs to be broken up into higher resolution chunks, resulting in better code. The remaining (rare) cases I just allow to exceed the limit.

Posted by Mojapo on March 13, 2008 at 10:15 AM PDT #

Post a Comment:
Comments are closed for this entry.
About

mduigou

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today