Документация
HTML CSS PHP PERL другое

Section 2.10.  Indentation

 
Previous
Table of Contents
Next

2.10. Indentation

Use four-column indentation levels.

Indentation depth is far more controversial than line width. Ask four programmers the right number of columns per indentation level and you'll get four different answers: two-, three-, four-, or eight-column indents. You'll usually also get a heated argument.

The ancient coding masters, who first cut code on teletypes or hardware terminals with fixed tabstops, will assert that eight columns per level of indentation is the only acceptable ratio, and support that argument by pointing out that most printers and software terminals still default to eight-column tabs. Eight columns per indentation level ensures that your code looks the same everywhere:

    while (my $line = <>) {
            chomp $line;
            if ( $line =~ s{\A (\s*) -- ([^\n]*) }{$1#$2}xms ) {
                    push @comments, $2;
            }
            print $line;
    }

Yes (agree many younger hackers), eight-column indents ensure that your code looks equally ugly and unreadable everywhere! Instead, they insist on no more than two or three columns per indentation level. Smaller indents maximize the number of levels of nesting available across a fixed-width display: about a dozen levels under a two- or three-column indent, versus only four or five levels with eight-column indents. Shallower indentation also reduces the horizontal distance the eye has to track, thereby keeping indented code in the same vertical sight-line and making the context of any line of code easier to ascertain:

    while (my $line = <>) {
      chomp $line;
      if ( $line =~ s{\A (\s*) -- ([^\n]*) }{$1#$2}xms ) {
        push @comments, $2;
      }
      print $line;
    }

The problem with this approach (cry the ancient masters) is that it can make indentations impossible to detect for anyone whose eyes are older than 30, or whose vision is worse than 20/20. And that's the crux of the problem. Deep indentation enhances structural readability at the expense of contextual readability; shallow indentation, vice versa. Neither is ideal.

The best compromise[*] is to use four columns per indentation level. This is deep enough that the ancient masters can still actually see the indentation, but shallow enough that the young hackers can still nest code to eight or nine levels[Section 2.10.  Indentation] without wrapping:

[*] According to the research reported in "Program Indentation and Comprehensibility" (Communications of the ACM, Vol. 26, No. 11, pp. 861-867).

[Section 2.10.  Indentation] But don't do that! If you need more than four or five levels of indentation, you almost certainly need to factor some of that nested code out into a subroutine or module. See Chapters Chapters 9 and 17.


    while (my $line = <>) {
        chomp $line;
        if ( $line =~ s{\A (\s*) -- (.*)}{$1#$2}xms ) {
            push @comments, $2;
        }
        print $line;
    }

    Previous
    Table of Contents
    Next
    © 2000- NIV