Download Ada in Action

Transcript
Chapter 4. Programming isn't Software Engineering
119
4.6.15. Building from the Bottom
I haven't said so, but the PLAYING_CARDS package is a bottom-up design. I tried to keep this a secret
until now because bottom-up design is frowned upon in some circles. It earned a bad reputation because
undisciplined programmers often start at the bottom of a design and keep building. When you start at
multiple roots, you have to be incredibly lucky for all of these roots to grow together into a neat solid
trunk. Normally there is a burl where things that don't really fit together have been forced into place. A
pure bottom-up design usually isn't very good.
That doesn't mean all bottom-up designs are bad. I've shown you how you can build a
SCROLL_TERMINAL on top of a
VIRTUAL_TERMINAL that is built on top of operating-system
interface packages. That's a bottom-up design, and it's good. Bottom-up design helps you write basic utility
programs that can be used as building blocks in many different programs.
You only get into trouble when you try to get these individual building blocks to continue to grow and
somehow merge with each other to form one program. You just can't start from many places and expect to
be able to join them all with one golden spike (6). They probably aren't going to line up. The key to success
is to recognize when you have built all the foundation modules you need, then stop working from the
bottom-up.
4.6.16. Top-Down Design
When it comes time to establish the program flow, I think it is best to start from the top and work down.
This means stating the solution to the problem in the most general terms, then defining those general
terms with more specific terms until the solution is spelled out in complete detail. We saw an example of
this in Chapter 3.5.9, and here is another example.
Listing 68 shows the top level of the Draw_Poker program. Don't think for a moment it was written
sequentially. I used a screen-oriented editor and jumped all over that file. I started with a top level skeleton
and put flesh on it. That is, it began like this:
procedure Draw_Poker is
begin
loop
(cursor)
end loop;
end Draw_Poker;
I knew I wanted an infinite loop. I just had to decide what to put in the loop. I began with comments
derived directly from the requirements.
---------
Play as long as the user is willing to bet.
Shuffle before every deal.
Deal a hand to the player.
Show him what he has.
Let the player hold or draw each card.
Replace any cards he may have discarded.
Show him what he has now.
Pay him if he won.
6 Note to international readers who might be unfamiliar with American history. Two companies were
given the task of building the first transcontinental railroad. One started from the East, the other started
from the West. When they met in the middle, the last two sections of track were joined by a golden spike.
Ada in Action