CSC 288 · Structured Programming
Our notes
2 min readStepwise refinement (Wirth, 1971)
Start with a one-line statement of the task, then expand each step into smaller steps until every step is a simple statement in the language. Refine the data at the same time: decide what is stored, then how.
Worked example: a class result summary
Level 0: Produce a summary of a class's scores.
Level 1:
- Read the scores.
- Compute the average, highest and lowest.
- Count the passes.
- Print the summary.
Level 2 for step 2:
- total := 0; highest := first score; lowest := first score
- for each score: add to total; compare with highest and lowest
- average := total / count
Data refinement: "the scores" becomes an array of integers of size MaxStudents, plus a count n.
Each level is checked before the next is written.
Modularisation
A module (procedure, function, unit) does one well-defined job. Good modules have:
- High cohesion: everything inside belongs to that one job.
- Low coupling: few, clear connections to other modules (parameters, not shared globals).
- Information hiding: other modules use it through its interface without knowing its insides.
Three approaches
- Top-down: design and code the main program first, using stubs (dummy modules) for the parts below; replace them one by one. The overall structure is tested early.
- Bottom-up: write and test the low-level modules first (reading data, sorting), then combine them with driver programs. Reliable building blocks; the overall design is tested late.
- Nested virtual machines (layers): build a series of layers, each a "machine" offering operations to the layer above. Example: layer 1 offers ReadRecord and WriteRecord on a file; layer 2 offers AddStudent and FindStudent using layer 1; layer 3 is the registration program using layer 2. A change inside a layer (say, from a text file to a database) does not touch the layers above.
Real projects mix them: top-down design, bottom-up coding of the utilities.
A structure chart
A diagram of modules as boxes, with the main program at the top and lines down to the modules it calls; small arrows show the data passed. It is the map of the top-down design.
Exams usually ask
- Explain stepwise refinement with an example.
- Compare top-down and bottom-up approaches; define stubs and drivers.
- Explain cohesion, coupling and information hiding.
Common mistake
Refining only the statements. Wirth's point was to refine the data too; choosing a poor data structure early makes every later step harder.
Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.
No reading links here yet
Our team adds free books, videos and practice for each topic, and opens every link before it goes up.