Still thinking about method, which is just as important as language, let us take another look at CATMOUSE EXEC.
The program is about a cat and a mouse and their positions in a corridor. At some stage their positions will have to be pictured on the screen. The whole thing is too complicated to think about all at once; the first step is to break it down into:
Now let us look at main program. The user (who plays the mouse) will want to see where everybody is before making a move. The cat will not. The next step is to break the main program down further, into:
Do forever
call Display
Mouse's move
Cat's move
end
Conclusion
The method for designing loops is to ask two questions:
Well, the loop terminates (and the game ends) when:
At the end of the program, the user must be told what happened.
call display
say who won
Putting all this together, we have:
/*------------------------------------------------------*/
/* Main program */
/*------------------------------------------------------*/
do forever
call display
/*---------------------------------------------------*/
/* Mouse's turn */
/*---------------------------------------------------*/
...
if mouse = hole then leave /* reaches hole */
if mouse = cat then leave /* hits cat */
/*---------------------------------------------------*/
/* Cat's turn */
/*---------------------------------------------------*/
...
if cat = mouse then leave
end
/*------------------------------------------------------*/
/* Conclusion */
/*------------------------------------------------------*/
call display
if cat = mouse then say "Cat wins"
else say "Mouse wins"
exit
/*------------------------------------------------------*/
/* Subroutine to display the state of play */
/* */
/* Input: CAT and MOUSE */
/* */
/*------------------------------------------------------*/
display:
...
The method that we have just discussed is sometimes called stepwise refinement. You start with a specification (which may be incomplete). Then you divide the proposed program into routines, such that each routine will be easier to code than the program as a whole. Then you repeat the process for each of these routines until you reach routines that you are sure you can code correctly at the first attempt.
While you are doing this, keep asking yourself two questions:
Granny is going to knit you a warm woolen garment to wear when you go sailing. This is what she might do.
Each of these jobs is simpler to describe than the job of knitting a pullover. In computer jargon, breaking a job down into simpler jobs is called stepwise refinement.
At this stage, look at the specification again. A sailor might need to put on the pullover in the dark, quickly, without worrying about the front or back. Therefore, the front should be the same as the back; and the two sleeves should also be the same. This could be programmed:
do 2
CALL Knit_body_panel
end
do 2
CALL Knit_sleeve
end
CALL sew_pieces_together
In programming, the best method is to go on refining your program, working from the top, until you get down to something that is easy to code.
Top down is the best approach.
When you are refining your program, your objective is to make each piece simpler. This almost certainly means:
If your pieces really are simpler, they will probably have simpler names, too. For instance:
rather than