Definition
Stepwise refinement is the engineering technique of writing a program as a sequence of progressively more detailed versions, each fleshing out one layer that the previous version left as a stub. The first version names the high-level structure in plain prose or empty function calls. Each subsequent version replaces one stub with its real implementation, possibly introducing new lower-level stubs as it goes. The work terminates when every name resolves to either a built-in operation or a fully written function.
Niklaus Wirth named the method in a 1971 paper and used it to argue that good program design proceeds from the outside in rather than the bottom up. The technique is older than its name — it is essentially structured outlining applied to code — and it works in every language that supports procedural decomposition, which is to say, every modern language.
Why it matters
How it works
Start by writing the program as if every operation you wish you had already existed. The outermost layer is often a handful of lines that read like a paragraph of prose with function names instead of verbs. Each named operation is a stub: an empty function whose body is either a comment, a fixed return value, or a print statement that announces the intent. The program does not yet work, but the structure is visible and reviewable.
Pick one stub and ask what it needs to do. If the answer is short and concrete, write it. If the answer is itself a small algorithm, write that algorithm using new stub calls for whatever subordinate operations it needs. Repeat. Each refinement step is local — you only need to hold one function in your head — and each step makes the program slightly more real. The final pass produces working code, but more importantly it produces code whose shape mirrors the design history: outer layers describe intent, inner layers handle mechanism. A reader who knows nothing about the implementation can still understand the program by reading from the top down.