Line length in programming

Christian Kruse

In the past I was a fan of long lines. I thought that modern editors are perfectly able to virtually wrap lines so that the code remains readable even when the lines exceed screen length. But since I began working on the PostgreSQL project I realized that short lines are much better readable nonetheless. You eyes don't get „lost“ and have to re-focus. Also it is easier to keep track of the surrounding context (which is important, particularly in programming).

So I did some research. And I found out that it seems to be a well known fact in the typographer environment that most people can read no more than 10 to 12 words per line before they have trouble differentiating lines from each other. For this assumption a word has an average length of 5 characters. While I think that this is too small (12 words with 4 characters each means we should add a line break at 60 characters) a relaxed version with 25% - 50% increase brings us to 15 words per line and thus 75 characters. Very close to the „80 characters per line“ rule of thumb propagated by the gray-bearded ones.

So to summarize: capping line lengths at 80 characters still makes sense. It improves the ability to read the source code and to scan through the source. It also increases the overall overview about the source code. I for one enabled highlighting of long lines (lines with more than 80 characters) in my Emacs:

<pre class=”brush: elisp”>(require ‘whitespace)(global-whitespace-mode t)(setq whitespace-line-column 79)(setq whitespace-style ‘(face lines-tail))</pre></code><p> This results – with my color theme – in a nice red highlighting of the trailing part of the line:</p><p> <img src=”” alt=”Image of a long line highlighted in red”></p>