Line Length, Density and Volume

TAIT's Organization

T8s.Org: Tait's; TAIT's; and Taits!

 
 
 
 

Archives

Categories

Line Length, Density and Volume

We mentioned Line Length, Density and Volume in our introduction, So it just seems like a good place to start this Logical Coding Standard. We call this the TAIT Style!

Let us define what each one of these means:

Line Length

Line Length is simply the number of characters on a line. For the line length we set one statement, or 70 Characters, whichever it less. Obviously we are not counting blank lines, which have no statements nor characters!

Line Length = 70 or Less!

Density

Density is simply the number of statements you have on one line. Ideally you would want one statement per line. For this purpose, control structures are counted as one statement.  For example, the following have a density of one.

Logical:
if ( '100' == $b )
    $a = $b;

And…

Logical:
if ( ( condition1 ) || ( condition2 ) ) {
    action1;
} elseif ( ( condition3 ) && ( condition4 ) ) {
    action2a;
    action2b;
} else {
    defaultaction;
}

Density should be one statement per line, unless line-length requires the density to be higher. Another In this example the density is 3 (or 4?) and the following code, which is much more readable, had a density of one.

Illogical:
list($foo, $bar, $baz) = array(Zim::getVal('foo'), Dib::getVal('bar'), Gir::getVal('baz', Gir::DOOM));
Logical:
$foo = Zim::getVal('foo');
$bar = Dib::getVal('bar');
$baz = Gir::getVal('baz', Gir::DOOM);

If for some reason you needed to, you could still use the exact same statements, however… Adding More Code that does nothing is…

Illogical:
list($foo, $bar, $baz) = array(
    Zim::getVal('foo'),
    Dib::getVal('bar'),
    Gir::getVal('baz', Gir::DOOM)
);

Density = One Statement per Line, unless Line Length > 70.

Volume

Density is the number of statements per line, Volume is the number of lines per statement! You want both to be close to 1 to 1!

If you think of filling a jar, as it fills up (stacks up with sand?) what is inside is its volume. Basically it is getting higher and higher. Well, the Volume of Lines is also getting taller. Volume basically means the number of lines. But it is not the total number, it is the number of lines expresses as a ratio to the number of statements. In a perfect world, it would be one, or 1 to 1!

One line to one statement. (Just like density is perfectly one statement per line!)

Some people think if long lines are bad, then they should make really short lines. Consider the code from above in the Density and how it could be written with a high volume…

Not Logical:

Illogical:
$foo
=
Zim::getVal(
'foo'
);

$bar
=
Dib::getVal(
'bar'
);

$baz
=
Gir::getVal(
'baz'
,
Gir::DOOM
);

What a nightmare that would be to maintain!

Line Volume = One Line per Statement, unless Line Length > 70

70 Characters

As you no doubt noticed, everything keeps coming back to that line length of 70 Characters. There is a very good and logical reason for this and it has nothing to do with the width of your monitor, nor the Vi, Vim or Emacs editors! Most PHP code written, maintained or modified today is not written on these editors now. Likewise 80 Characters have been around a long time and word wrap is perfected already!

What 70 characters has to do with is humans, our ability to absorb data and understand it. Let’s take a look at this…

Jyoti Sanyal’s Indlish has an interesting and informative chapter titled ‘Shrink or sink’ on the length of the sentence. He writes: “Based on several studies, press associations in the USA have laid down a readability table. Their survey shows readers find sentences of 8 words or less very easy to read; 11 words, easy; 14 words fairly easy…” After that it starts getting much more difficult.

If you considered the standard 5 characters per average printed word, each line should be 14 * 5 or 70 Characters per line!

Printing and publishing typographers figured out a long time ago that most people can read no more than 10 to 12 words per line before they have trouble differentiating lines from each other. 12 * 5 = 60 Characters per line!

Another way to look at this is that most people comprehend sentences of about 25 syllables, either reading or listening! The average syllable is about 2.5-2.8 characters each. That would give a comprehension range of about 62.5 – 70 Characters!

Not only that, they also discovered that Polysyllabic words, or unusual punctuation, are likely to make the meaning of the document difficult to grasp. Sort of describes coding, doesn’t it?

The Logical Structure

Now Consider the Logical Structure of Coding. People can easily relate to the fact that virtually no one reads a line of a beloved novel, at the same speed as reading the same length line of a complex mathematical equation! It is fairly certain that it is more difficult to read a line of computer code than to read the same length line of a novel or history textbook.

Therefore, it is logical to assume that the natural text limits 70 Characters sighted above, would be more of a maximum and most lines of code should be shorter than 70 Characters.

Statements or Paragraphs

Likewise, Computer code is read and executed in a very rigid order, basically one statement at a time. Although we group computer code into functions and classes, these should not be compared to or misunderstood to be like paragraphs or chapters. Natural writing (story writing) and Code Writing are very different in this aspect.

Therefore, readability for Code Reading means understand each statement, and in the sequence these statements are in. Understanding these small units of code, rather than grasping the concepts expressed in the larger paragraphs and chapters of novels.

Logical Conclusion

The Logical Conclusion to this, is to limit line length to no more than 70 Character per line, and to bring Density and Volume, as much as possible, into a 1:1 balance without going past the 70 Character limit.
complex mathematical equation, computer code, and lines of a novel should, indeed, must be written in different ways for maximum understanding.


Paul M. Jones has some thoughts about the 75 Character line limit PEAR recommends. Although I really believe in the 70 Characters or less limit, his reasoning is only faulty in the number he chooses and the random 25% to 50% increase he adds for unexplained reasons. I find it amazing that his estimate is so near to what the experts I sighted say.