In order to make programs easier to understand, most programmers indent their
programs. The general rule for a C program is to indent one level for each new block
or conditional.
There are two styles of indentation, and a vast religious war is being raged in the
programming community as to which style is better. The first is the short form:
while (! done) {
printf("Processing\n");
next_entry();
}
if (total <= 0) {
printf("You owe nothing\n");
total = 0;
} else {
printf("You owe %d dollars\n", total);
all_totals = all_totals + total;
}
In this case, curly braces ({}) are put on the same line as the statements. The other
style puts the {} on lines by themselves:
while (! done)
{
printf("Processing\n");
next_entry();
}
if (total <= 0)
{
printf("You owe nothing\n");
total = 0;
}
else
{
printf("You owe %d dollars\n", total);
all_totals = all_totals + total;
}
Both formats are frequently used. Programmer should use the format which he/she feels most
comfortable with.
The amount of indentation is left to the programmer. Two, four, and eight spaces
are common. Studies have shown that a four-space indent makes the code most
readable. However, being consistent in your indentation is far more important than
the indention size you use.
Some editors, like the UNIX Emacs editor, the Turbo C++, Borland C++, and
Microsoft Visual C++ internal editors, contain code that automatically indents your
programs as you create them. Although these editor-based indentation systems are
not perfect, they do go a long way to helping you create properly formatted code.
Source:
No comments:
Post a Comment