Java Reference
In-Depth Information
it to 0 and then setting it to 0 again at the end of the outer repetend— is just not
good programming practice, as we will see in a moment.
The student's programming strategy
not gotten around to those yet.
My way of programming is to write the program first, and later to
fill in statement-comments and loop invariants and things.
He said this in spite of all our discussions of good programming practices and in
spite of all our examples of top-down design. And his practices led to an error
that took him a great deal of wasted time to find and to a bad design. In fact, pro-
viding comments after the fact is a waste of time.
When pressed for the invariant of the outer loop and for a statement-com-
ment for the repetend of the outer loop, after some time and with help, the stu-
dent said that the invariant was:
invariant: pairs (i, 0..C-1) with 0≤i<r have been processed
And along with this came the statement-comment for the repetend:
Process pairs (r, 0), …, (r, C - 1)
so that the segment could be interpreted as:
// Process pairs (r, c) , for 0≤r<R , 0≤r<C
int r= 0; int c= 0;
// inv: pairs (0..r-1, 0..C-1) have been processed
while (r != R) {
Process pairs (r, 0) , …, (r, C - 1)
r= r + 1; c= 0;
}
// Process pairs (r,c) , for 0≤r<R , 0≤r<C
int r = 0;
// invariant: pairs (0..r-1, 0..C-1) have been processed
while (r != R) {
// Process pairs (r, 0) , , (r, C - 1)
int c= 0;
// invariant: pairs (r, 0) , …, (r , c-1) have been processed
while (c != C) {
Process pair (r , c); c= c + 1;
}
r= r + 1;
}
Figure 7.8:
A well-designed segment to process pairs
Search WWH ::

Custom Search