6.8. Program Development

At this point, you should be able to look at complete functions and tell what they do. Also, if you have been doing the exercises, you have written some small functions. As you write larger functions, you might start to have more difficulty, especially with runtime and semantic errors.

To deal with increasingly complex programs, we are going to suggest a technique called incremental development. The goal of incremental development is to avoid long debugging sessions by adding and testing only a small amount of code at a time.

As an example, suppose you want to find the distance between two points, given by the coordinates (x1, y1) and (x2, y2). By the Pythagorean theorem, the distance is:

Distance formula

The first step is to consider what a distance function should look like in Python. In other words, what are the inputs (parameters) and what is the output (return value)?

In this case, the two points are the inputs, which we can represent using four parameters. The return value is the distance, which is a floating-point value.

Already we can write an outline of the function that captures our thinking so far.

def distance(x1: float, y1: float, x2: float, y2: float) -> float:
    return 0.0

Obviously, this version of the function doesn’t compute distances; it always returns zero. But it is syntactically correct, and it will run, which means that we can test it before we make it more complicated.

The test.equal function from the test module calls the distance function with sample inputs: (1.0, 2.0, 1.0, 2.0). The first 1.0, 2.0 are the coordinates of the first point and the second 1.0, 2.0 are the coordinates of the second point. What is the distance between these two points? Zero. test.equal compares what is returned by the distance function and the 0 (the correct answer).

Extend the program …

Write another unit test. Use (1.0, 2.0, 4.0, 6.0) as the parameters to the distance function. How far apart are these two points? Use that value (instead of 0) as the correct answer for this unit test.

Write another unit test. Use (0.0, 0.0, 1.0, 1.0) as the parameters to the distance function. How far apart are these two points? Use that value as the correct answer for this unit test.

The first test passes but the others fail since the distance function does not yet contain all the necessary steps.

When testing a function, it is essential to know the right answer.

For the second test the horizontal distance equals 3.0 and the vertical distance equals 4.0; that way, the result is 5.0 (the hypotenuse of a 3-4-5 triangle). For the third test, we have a 1-1-sqrt(2) triangle.

At this point we have confirmed that the function is syntactically correct, and we can start adding lines of code. After each incremental change, we test the function again. If an error occurs at any point, we know where it must be — in the last line we added.

A logical first step in the computation is to find the differences x2- x1and y2- y1. We will store those values in temporary variables named dx and dy.

def distance(x1: float, y1: float, x2: float, y2: float) -> float:
    dx: float
    dy: float

    dx = x2 - x1
    dy = y2 - y1

    return 0.0

Next we compute the sum of squares of dx and dy.

def distance(x1: float, y1: float, x2: float, y2: float) -> float:
    dx: float
    dy: float
    dsquared: float

    dx = x2 - x1
    dy = y2 - y1
    dsquared = dx**2.0 + dy**2.0

    return 0.0

Again, we could run the program at this stage and check the value of dsquared (which should be 25.0).

Finally, using the fractional exponent 0.5 to find the square root, we compute and return the result.

Fix the error …

Two of the tests pass but the last one fails. Is there still an error in the function?

Frequently we discover errors in the functions that we are writing. However, it is possible that there is an error in a test. Here the error is in the precision of the correct answer.

The third test fails because by default test.equal checks 5 digits to the right of the decimal point.

  • Change 1.41 to 1.4142135623730951 and run. Now the output matches.

Now all four the tests pass! Wonderful! However, you may still need to perform additional tests.

When you start out, you might add only a line or two of code at a time. As you gain more experience, you might find yourself writing and debugging bigger conceptual chunks. As you improve your programming skills you should find yourself managing bigger and bigger chunks: this is very similar to the way we learned to read letters, syllables, words, phrases, sentences, paragraphs, etc., or the way we learn to chunk music — from indvidual notes to chords, bars, phrases, and so on.

The key aspects of the process are:

  1. Make sure you know what you are trying to accomplish. Then you can write appropriate unit tests.

  2. Start with a working skeleton program and make small incremental changes. At any point, if there is an error, you will know exactly where it is.

  3. Use temporary variables to hold intermediate values so that you can easily inspect and check them.

  4. Once the program is working, you might want to consolidate multiple statements into compound expressions, but only do this if it does not make the program more difficult to read.

You have attempted of activities on this page
6.7. Flow of Execution Summary"> 6.9. Composition">Next Section - 6.9. Composition