[ Pobierz całość w formacie PDF ]

out the code somewhat.
Variations.
One variation on the standard indentation styles concerns if statements that affect a single line.
For example:
if (total > 1000)
printf("You owe a lot\n");
This style of indentation can create confusion, as illustrated by the following
example:
/* Problem code */
if (index
fprintf(stderr,"Error: Index out of range\n");
exit (8);
At first glance, it looks like the program will print an error message only if index is out of
range. (That's what the programmer intended.) But on closer inspection, you'll notice that there
are no braces enclosing the two statements under the if. In fact, the code is indented incorrectly.
c04.doc - 61 - Modified: January 9, 1999 12:16 am
C Elements of Style Draft Version 0.8 by Steve Oualline
Indented correctly, the code looks like this:
/* Problem code */
if (index
fprintf(stderr,"Error: Index out of range\n");
exit (8);
The problem is confusion between multi-line if controlled statements and single-line state-
ments. To solve this problem, put single-line statements and their ifs on the same line:
if (total > 1000) printf("You owe a lot\n");
This makes very clear that the if affects only one line. The problem is that it makes the printf
line a little more difficult to find and breaks the one-statement-per-line rule.
How much to indent
In this book I indent four spaces for each logic level. Why four? Here are some examples of
various indentations.
c04.doc - 62 - Modified: January 9, 1999 12:16 am
C Elements of Style Draft Version 0.8 by Steve Oualline
Two Spaces:
/* Short form indentation */
while (! done) {
printf( Processing\n );
next_entry();
}
if (total
printf("You owe nothing\n");
total = 0;
} else {
printf("You owe %d dollars\n", total);
all_totals = all_totals + total;
}
if (total > 1000)
printf("You owe a lot\n");
Four Spaces:
/* Short form indentation */
while (! done) {
printf( Processing\n );
next_entry();
}
if (total
printf("You owe nothing\n");
total = 0;
} else {
printf("You owe %d dollars\n", total);
all_totals = all_totals + total;
}
if (total > 1000)
printf("You owe a lot\n");
Eight Spaces:
/* Short form indentation */
while (! done) {
printf( Processing\n );
next_entry();
}
if (total
printf("You owe nothing\n");
total = 0;
} else {
c04.doc - 63 - Modified: January 9, 1999 12:16 am
C Elements of Style Draft Version 0.8 by Steve Oualline
printf("You owe %d dollars\n", total);
all_totals = all_totals + total;
}
if (total > 1000)
printf("You owe a lot\n");
The advantage of a smaller indent is that you don't run into the right margin as quickly. The
disadvantage is that it's hard to tell the various levels apart.
Larger indents are easier to read, but larger indents mean that you run out of room faster.
Several researchers have studied this problem in detail. They started with the same program
and indented it using different indent sizes. They then gave the various flavors of the program to a
set of graduate students and told them each to enhance it by adding some additional commands.
The students had never seen the program before. The researchers measured time amount of time it
took each student to understand and fix the program. As a result of this and other studies like it,
they concluded that four spaces is the ideal indentation.
Rule 4-19:
The best indentation size is four spaces.
c04.doc - 64 - Modified: January 9, 1999 12:16 am
C Elements of Style Draft Version 0.8 by Steve Oualline
c04.doc - 65 - Modified: January 9, 1999 12:16 am [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • jagu93.xlx.pl
  •