Layout
convention: placement of braces "{" "}" for compound statements
There are a number of conventions for
the layout of the braces of compound statements. Although most people agree
that it is easier to read a program when the open and corresponding close
braces are lined up vertically (i.e. tabbed to the same degree), such an
arrangement means that the code is longer. More lines of code mean that
less fits on to a screen, and the longer a piece of code, the harder it
is to understand.
Since for this module you'll be writing
relatively short programs, we will recommend you follow the convention
of having the open and close brace for a compound statement aligned vertically,
and on a separate line of their own. When you move on to writing very large
programs, you may wish to adopt a different brace layout convention.
Consider this loop statement from one
of the bouncing ball applications investigated in unit 2:
for(
int i = 0; i < balls.length; i++){
balls[i].paint(
g );
balls[i].move();}
rBall.paint(
g );
rBall.move();
In fact, due to the indentation convention
followed, it is reasonably clear which statements are to be repeated in
the loop. However, we can make it even clearer that there is a compound
statement, and that the open and close braces are both present,
by placing the braces on their own lines:
for(
int i = 0; i < balls.length; i++)
{
balls[i].paint( g );
balls[i].move();
}
rBall.paint( g );
rBall.move();
You should layout the compound statement
of all iteration statements, selection statements, and other compound statements
in a similar way. Another example of this convention, this time of with
an ifstatement,
is as follows:
if(
invalidPINEntered )
{
disp("Error: Personal Identity Number
not recognised-transaction cancelled");
cancelTransaction();
}
else
{
displayMessage("Personal Identity Number confirmed");
processTransaction();
}
Back
to top
RITSEC - Global Campus
Copyright © 1999 RITSEC- Middlesex
University. All rights reserved.
webmaster@globalcampus.com.eg
|