What is stepwise refinement?
The verb refine means to improve a method, plan, system etc., gradually making slight changes to it. The word stepwise means to do something in a series of distinct stages. So, stepwise refinement is a process of breaking down the steps of an outline solution into smaller steps and repeat the process for each smaller step until they are all detailed enough.
Using stepwise refinement, we improve our solution to a problem gradually by breaking down each step into smaller ones and adding more details, and we continue the breaking down until all the smaller step are detailed enough. It is also known as top-down design. The outline solution is the top-level solution and the much smaller steps are the low-level solution with details. The outline solution contains less or no details and it is unimplementable. While the much smaller steps are full of details, it can be implemented easily. So, this approach starts from the outline solution, the top-level, solution to detailed steps, the low-level. The low-level steps are so detailed that you can write program code from it directly.
Example
The problem to be solved: Take as input a chosen symbol and a number. Output a triangular flag which is made up of the symbol, with the number of lines matching the number input.
For example, the input symbol and number 5 result in the following output:
We can write an outline solution in pseudocode from the above example output easily:
INPUT Symbol, Number
Line <- 1
WHILE Line <= Number
Output a line
Line <- Line + 1
ENDWHILE
The steps are numbered so that they can be referred to later easier.
Line 01,02,03,05,06 are easy. We can translate them into python code without any effort. But line 04 is a bit more complicated. Each line of the flag contains different numbers of asterisks. Besides, a new line shall start after those asterisks.
We can see that the number of asterisks of each line is exactly the number of the line. So, we can write more pseudocode for the line 04:
Count <- 1
WHILE Count <= Line
output Symbol
Count <- Count + 1
ENDWHILE
Output NewLine
The above pseudocode gives more details about how to output a line. It can be translated into python code easily. Now, we can replace line 04 with the above segment:
INPUT Symbol, Number
Line <- 1
WHILE Line <= Number
Count <- 1
WHILE Count <= Line
output Symbol
Count <- Count + 1
ENDWHILE
Output NewLine
Line <- Line + 1
ENDWHILE
and write the python code:
Symbol = input("Enter a Symbol:")
Number = int(input("Enter a number:"))
Line = 1
while Line <= Number:
Count = 1
while Count<= Line:
print(Symbol,end='')
Count = Count + 1
print()
Line = Line +1
Summary
Let's summarize the process:
- We write an outline solution to the problem
- We split the step output a line into 6 smaller steps
- Until now, the steps of the solution are all detailed enough
- We write python code directly from the pseudocode
Once again, stepwise refinement is a process of breaking down an outline solution into smaller steps, and repeat the process until all the smaller steps are detailed enough.