# Stepwise Refinement

## Stepwise Refinement by Functional Decomposition

• Stepwise refinement: design a problem solution by
1. stating the solution at a high level
2. refining steps of the solution into simpler steps
3. repeating step 2, until steps are simple enough to execute
• Decompose based on function of each step
• Makes heavy use of pseudocode

## Example

• Problem: Print the count and average of a sequence of positive integers

• Initial Solution:
1. Initialize data
2. Get data
3. Calculate results
4. Output results

• Refinement:
1. Initialize data
2. get data
• 2.1 loop
• 2.2     get integer x
• 2.3     exit when x < 1
• 2.4     process x
3. Calculate results
• 3.1 avg = sum / count
4. Output results

• Further refinement (of step 2.4):
• 2.4.1 increase count by 1
• 2.4.2 increase sum by x

• Process of refinement is complete when all steps can be easily programmed

## Application to Module Design

• Same technique can be used to decompose a system into modules
1. Begin with main module
2. Break a module into submodules
3. Repeat step 2 until modules are small and easily written
4. Use stepwise refinement on individual modules

## Module Design Goal

• According to David Parsons (and others) a good design should
• Maximize Cohesion
• Minimize Coupling

• Cohesion: Procedure have one well defined task
• Coupling: Don't access global data or data from another module

• 324 Students: Does this sound familiar?

## Structure chart

1. Modules

2. Parameters and return values
1. logical
2. data

3. If and loop

## Example

• Update customer master file with transaction records

## Top Down and Bottom Up

• Note that the example above is a top down solution

• Top down: solve a problem by breaking a solution into smaller and smaller parts

• Bottom up: solve a problem by joining simpler functions to make more and more complex functions

• Most people use a combination of top down and bottom up