Complex problems can thus be solved using top-down design,
also known as stepwise refinement, where we:
1. Break the problem into parts.
2. Then break the parts into parts.
3. Solve each of the parts, which will be easy to do.
Breaking the problem into parts helps us to clarify what needs to be
done, and at each step of refinement, the new parts become less
complicated and therefore easier to figure out. Thus parts of the
solution may turn out to be reusable. Breaking the problem into
parts also allows more than one person to work on the solution. By
separating the low-level work from the higher level objects, the
design becomes modular, which means development can be
self-contained. A top-down modular approach means you create a
“skeleton” code that illustrates clearly how low-level modules inte-
grate. This approach also reduces coding errors, because each
module has to be processed separately.
2.5.3 Non-MEL Example of Top-Down Design
Problem
n
We own a home improvement company.
n
We do painting, roofing, and basement waterproofing.
n
A section of town has recently flooded (zip code 21222).
n
We want to send out pamphlets to our customers in that area.
Top-Down Solution
1. Get the customer list from a file.
2. Sort the list according to zip code.
3. Make a new file of only the customers with the zip code 21222
from the sorted customer list.
4. Print an envelope for each of these customers.
Chapter 2
52
Further Breakdown
n
Should any of these steps be broken down further? Possibly.
n
How do I know? Ask yourself whether or not you could easily
write the algorithm for the step. If not, break it down again.
n
When you are comfortable with the breakdown, write the
pseudocode for each of the steps (modules) in the hierarchy.
n
Typically, each module will be coded as a separate function.
Another example of top-down design:
Problem
Write a program that draws a picture of a house.
Solution
1. Draw the outline of the house.
2. Draw the chimney.
3. Draw the door.
4. Draw the windows.
Pseudocode
n
Call Draw Outline
n
Call Draw Chimney
n
Call Draw Door
n
Call Draw Windows
Some Observations
The door has both a frame and knob. We could break this into two
steps. But don’t the windows look the same? They just have differ-
ent locations. We can reuse the code that draws a window. Simply
copy the code three times and edit it to place the window in the cor-
rect location, or use the code three times, “sending it” the correct
location each time. This is an example of code reuse.
Conditional Statements, Iterations, and Procedures
53
Chapter 2

Get Professional MEL Solutions for Production now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.