9.2.4 Stepwise Refinement: From Outline to Programmable Detail
A first solution often describes what should happen without explaining exactly how a computer could carry it out. Stepwise refinement develops that outline through several levels, replacing broad instructions with precise input, processing, decisions, repetition and output.
By the end of this section, you should be able to:
- Explain why an outline algorithm may need further detail.
- Refine a broad instruction through more than one level.
- Recognise when a step is precise enough to translate into a program.
- Preserve the purpose and order of an algorithm while detail is added.
- Use input, output, assignment, sequence, selection and iteration in a refined solution.
- Check a refined algorithm for missing data, ambiguous conditions and incorrect dependencies.
- Distinguish stepwise refinement from abstraction and decomposition.
What Is Stepwise Refinement?
Stepwise refinement begins with a high-level description of a solution. A designer selects one broad step, replaces it with smaller steps, and repeats the process wherever more precision is needed. Each new level should explain the same solution more clearly rather than changing its purpose.
For example, process each greenhouse zone
is useful in an early outline, but it does not state
which values are read, how a watering decision is made, how the amount is calculated or what is output.
Refinement exposes those details one layer at a time.
A Repeatable Refinement Process
- State the required result. Make sure the overall purpose is clear.
- Write a high-level outline. Use a small number of meaningful actions.
- Choose an unclear step. Look for verbs such as process, check, prepare or handle.
- Expand that step. Identify its data, calculations, conditions, repeated actions and outputs.
- Check dependencies. Confirm that every value exists before it is used.
- Repeat where necessary. Continue until each remaining instruction is unambiguous and implementable.
- Trace the result. Use sample data to confirm that the refined version still meets the original purpose.
When Is a Step Detailed Enough?
A step is usually ready for implementation when another programmer would not need to make an important design decision to translate it into code.
| Check | Question to ask | Evidence of sufficient detail |
|---|---|---|
| Data | Which values are needed and where do they come from? | Inputs and stored values have clear identifiers and units. |
| Processing | What calculation or update is performed? | The formula or assignment is stated explicitly. |
| Decision | What exact condition chooses a path? | The condition evaluates to TRUE or FALSE. |
| Repetition | What repeats, and when does repetition stop? | The loop body and control rule are both defined. |
| Order | Are values produced before later steps use them? | Dependencies are respected throughout the sequence. |
| Result | What is output and for whom? | The required output is explicit and complete. |
calculate the resultand
check the readingnormally need refinement because the calculation and condition are still missing.
Worked Scenario: Greenhouse Watering Planner
A greenhouse is divided into several growing zones. For each zone, the system receives its area and current soil-moisture percentage. A zone requires watering when its moisture is below 38%. A dry zone receives 1.7 litres per square metre. The algorithm must display the recommendation for every zone and the total water required for the whole greenhouse.
Initial outline
1. Obtain the run settings.
2. Process every growing zone.
3. Report the total water required.
This outline shows the overall sequence, but the second step hides most of the design. The next sections refine it without changing the original goal.
Building the Refinement Levels
Level 1: expand the three main stages
1. Obtain the run settings.
1.1 Input how many zones will be processed.
1.2 Set the greenhouse water total to zero.
2. Process every growing zone.
2.1 Repeat once for each zone.
2.2 Input the zone's area and moisture reading.
2.3 Decide whether watering is required.
2.4 Calculate the water for this zone.
2.5 Display the zone recommendation.
2.6 Add the zone amount to the greenhouse total.
3. Report the total water required.
3.1 Output the greenhouse water total.
Level 1 reveals the repeated work, but steps 2.3 and 2.4 still leave important decisions to the programmer.
Level 2: refine the decision and calculation
2.3 Decide whether watering is required.
2.3.1 Compare MoisturePercent with 38.
2.4 Calculate the water for this zone.
2.4.1 IF MoisturePercent < 38 THEN
WaterLitres ← AreaM2 * 1.7
ELSE
WaterLitres ← 0
ENDIF
The threshold, operator, formula and alternative result are now explicit. These details can be written directly using assignment and selection.
| Identifier | Role | Purpose |
|---|---|---|
NumberOfZones | Input | Stores how many zones must be processed. |
ZoneCounter | Control | Tracks the current repetition. |
AreaM2 | Input | Stores the area of the current zone in square metres. |
MoisturePercent | Input | Stores the current zone's moisture reading. |
WaterLitres | Process/output | Stores the calculated water for the current zone. |
TotalWaterLitres | Accumulator | Stores the sum of all zone requirements. |
Complete Programmable-Level Algorithm
INPUT NumberOfZones
TotalWaterLitres ← 0
FOR ZoneCounter ← 1 TO NumberOfZones
INPUT AreaM2
INPUT MoisturePercent
IF MoisturePercent < 38 THEN
WaterLitres ← AreaM2 * 1.7
ELSE
WaterLitres ← 0
ENDIF
OUTPUT ZoneCounter, WaterLitres
TotalWaterLitres ← TotalWaterLitres + WaterLitres
NEXT ZoneCounter
OUTPUT TotalWaterLitres
The refined algorithm now states the input, initialisation, loop boundary, decision, calculation, per-zone output, running-total update and final output. No major design choice is left unstated.
TotalWaterLitres must be initialised before the loop. If it is
reset inside the loop, the final output will contain only the most recent zone's amount.
Check That Refinement Preserved the Solution
Adding detail can introduce errors. Compare each refined level with the original problem and trace representative data before accepting the design.
| Zone | Area (m²) | Moisture (%) | Condition | Water (L) | Running total (L) |
|---|---|---|---|---|---|
| 1 | 12 | 29 | 29 < 38 → TRUE | 20.4 | 20.4 |
| 2 | 8 | 52 | 52 < 38 → FALSE | 0.0 | 20.4 |
| 3 | 10 | 33 | 33 < 38 → TRUE | 17.0 | 37.4 |
Useful checking questions
- Is every requirement from the problem represented?
- Does each refined group still perform the broad step it replaced?
- Are threshold values, units and boundaries correct?
- Can the loop terminate after the intended number of zones?
- Are all values initialised before use?
- Does tracing normal and boundary data produce sensible results?
Abstraction, Decomposition and Refinement
| Technique | Main purpose | Question it answers | Greenhouse example |
|---|---|---|---|
| Abstraction | Remove irrelevant detail. | What information matters for this task? | Use area and moisture, but ignore the colour of the plant labels. |
| Decomposition | Divide the overall problem into sub-problems. | What separate parts must be solved? | Separate data collection, zone processing and reporting. |
| Stepwise refinement | Add detail to an outline solution. | Exactly how will each broad step be carried out? | Replace “process a zone” with input, decision, calculation, output and update steps. |
Interactive: Refinement Explorer
Use the first mode to reveal the greenhouse solution one level at a time. Then switch to the trace mode to watch the refined algorithm process three sample zones and build its running total.
Common Mistakes and Misconceptions
- Replacing a broad step with another equally vague phrase.
- Adding unrelated features that change the original problem.
- Refining calculations but leaving conditions or loop boundaries undefined.
- Using a value before the step that creates or inputs it.
- Forgetting an alternative path when a condition is false.
- Initialising a counter or total inside the repeated block.
- Stopping after one level even though important design decisions remain.
- Assuming that more detail automatically means correct detail.
Practice
Try these original questions
- Explain stepwise refinement without using the phrase “break a problem down”.
- Why is
check whether the parcel can be collected
not yet programmable detail? - Refine
calculate the journey charge
when the charge is a £1.80 starting fee plus £0.32 per minute. - Write a high-level three-step outline for processing attendance at an after-school club.
- Refine the step
process each attendee
so that it includes input, a decision and an output. - For the greenhouse algorithm, calculate the water and running total for a fourth zone with area 9 m² and moisture 38%.
- Identify two errors that would occur if the total were set to zero inside the loop.
- Explain one difference between decomposition and stepwise refinement.
- Create two refinement levels for a system that inputs five noise readings and reports how many exceed 65 dB.
- Describe how you would decide that your final refinement is detailed enough to implement.
Review
| Question | A strong answer should include |
|---|---|
| What is refined? | The steps of an outline algorithm. |
| Why refine? | To remove ambiguity and reach implementable detail. |
| What may appear in the final level? | Input, output, assignments, sequence, conditions and loops. |
| What must remain unchanged? | The original purpose and required result. |
| How is the design checked? | Compare with requirements, inspect dependencies and trace sample data. |
| How does it differ from decomposition? | Refinement adds detail to steps; decomposition separates the overall problem into parts. |