Think Like a Programmer: Stepwise Refinement
Tags: tutorial, beginners, java, programming
Published: 2021-10-30 20:12:43

---
title: Think Like a Programmer: Stepwise Refinement
published: true
description: Think Like a Programmer: Stepwise Refinement
tags: tutorial, beginners, java, programming
//cover_image: https://direct_url_to_image.jpg
---
I see lots of posts about how to do *something* in a particular programming language, for example:

- How to center a `<div>` element
- How to use JavaScript promises
- How to create random numbers in Python

But I rarely see posts about *how to develop a program*. Any tutorial or online resource can explain programming syntax. That is only part of the battle. Knowing how to choose, combine, and organize programming statements to solve a programming problem is the real challenge.

One way to develop a program is what I call the *all or none* approach. It is straightforward:

- Think about a solution
- Code the entire program: Line 1, 2, 3, ...
- Execute, test, and revise the program until it there are no more errors

One problem with this approach is that the program may encounter numerous errors, making it difficult to debug and fix. There is a better way.

**Stepwise refinement** is a programming technique where you start by looking at the problem at a high level. Then you break down the problem into smaller, more manageable components. Next, you develop and refine each component, at each level, step by step. Compile, execute, and test every step of the way. The process continues until all components are developed, with no need to break them down any further. It is an iterative approach where you develop the program a little bit at a time.

Here is an example of stepwise refinement. Consider the following problem:

> 
Convert Fahrenheit temperature to Celsius temperature.

We will write this program in Java. Using stepwise refinement, we break down that problem into subtasks:

1. Accept Fahrenheit temperature as input.
1. Calculate Celsius temperature.
1. Output results.

We break down these subtasks even further. To *Accept Fahrenheit temperature as input*:

1. Prompt the user to enter a Fahrenheit temperature.
1. Accept the temperature value as input.
1. Store the temperature value in a variable.

At this point, we can write a small program that only prompts the user to enter the Fahrenheit temperature:

```java
public class Temperature { 
  public static void main(String[] args) { 
    System.out.print("Enter a Fahrenheit temperature: ");
  }
}
``` 

Now compile and execute the program. Make sure it works. Make sure the output is what you expect. If there are errors, it will be easy to debug, since the program is small.

Once you are certain that the program works properly, continue to the next subtasks. Write code to accept the input Fahrenheit temperature and store the value in a variable:

```java
import java.util.Scanner;  // NEW

public class Temperature { 
  public static void main(String[] args) { 
    System.out.print("Enter a Fahrenheit temperature: ");

    Scanner s = new Scanner(System.in);  // NEW
    double fahr = s.nextDouble();  // NEW
  }
}
```

Once again, compile, execute, and test. But you only need test the _new_ code, not the code from the previous iteration. Use a debugger or output statement to verify the value of the `fahr` variable. Make sure the input value is what you expect. This completes the *Accept Fahrenheit temperature as input* step.

Now write the code for the *Calculate Celsius temperature* step. With much of the program already written, this step only requires a little research to find the formula and just one new line:

```java
import java.util.Scanner;

public class Temperature { 
  public static void main(String[] args) { 
    System.out.print("Enter a Fahrenheit temperature: ");

    Scanner s = new Scanner(System.in);
    double fahr = s.nextDouble();

    double cels = (fahr - 32) * 5.0/9.0; // NEW
  }
}
```

Compile, execute, and test. Use a debugger or output statement to verify the value of the `cels` variable. Make sure the value is correct.

The last step is the _Output results_ subtask:

```java
import java.util.Scanner;

public class Temperature { 
  public static void main(String[] args) { 
    System.out.print("Enter a Fahrenheit temperature: ");

    Scanner s = new Scanner(System.in);
    double fahr = s.nextDouble();

    double cels = (fahr - 32) * 5.0/9.0;

    System.out.println(fahr + "degrees Fahrenheit is " + cels + " degrees Celsius."); // NEW
  }
}
```

Compile, execute, and test. Verify that the output statement is correct. If it is not, revise and repeat.

This sample problem may be simple enough to solve using the *all or none* programming approach. But the example demonstrates stepwise refinement, which is useful when solving more complicated programming problems. Break them down into more manageable subtasks and so on. Code a little bit at a time, execute, test, and repeat. Develop the program incrementally. Fix problems along the way. This will make complicated programming problems a lot easier to solve.

Thanks for reading. 😃

*Follow me on Twitter [`@realEdwinTorres`](https://twitter.com/realEdwinTorres) for more programming tips and help.*