Incremental development

Published

2023-08-01

Iterative and incremental development

Incremental development is a process whereby we build our program incrementally—often in small steps or by components. This is a structured, step-by-step approach to writing software. This approach has long been used to make the process of building complex programs more reliable. Even if you’re not undertaking a large-scale software development project, this approach can be fruitful. Moreover, decomposing problems into small portions or components can help reduce the complexity of the task you’re working on at any given time.

Here’s an example. Let’s say we want to write a program that prompts the user for mass and velocity and calculates the resulting kinetic energy. If you haven’t had a course in physics before, don’t sweat it—the formula is rather simple.

\begin{equation*} K_e = \frac{1}{2} m v^2 \end{equation*}

where K_e is kinetic energy in Joules, m is mass in kg, and v is velocity in m / s.

How would you go about this incrementally? The first step might be to sketch out what needs to happen with comments.1

"""
Kinetic Energy Calculator
Egbert Porcupine <egbert.porcupine@uvm.edu>
CS 1210
"""

# Step 1: Prompt user for mass in kg and save result
# Step 2: Prompt user for velocity in m / s and save result
# Step 3: Calculate kinetic energy in Joules using formula
# Step 4: Display pretty result

That’s a start, but then you remember that Python’s input() function returns a string, and thus you need to convert these strings to floats. You decide that before you start writing code you’ll add this to your comments, so you don’t forget.

"""
Kinetic Energy Calculator
Egbert Porcupine <egbert.porcupine@uvm.edu>
CS 1210
"""

# Step 1: Prompt user for mass in kg and convert input 
#         to float and save result
# Step 2: Prompt user for velocity in m / s and convert 
#         input to float and save result
# Step 3: Calculate kinetic energy in Joules using formula
# Step 4: Display pretty result

Now you decide you’re ready to start coding, so you start with step 1.

"""
Kinetic Energy Calculator
Egbert Porcupine <egbert.porcupine@uvm.edu>
CS 1210
"""

# Step 1: Prompt user for mass in kg and convert input 
#         to float and save result
mass = float(input('Enter mass in kg: '))
print(mass)
# Step 2: Prompt user for velocity in m / s and convert 
#         input to float save result
# Step 3: Calculate kinetic energy in Joules using formula
# Step 4: Display pretty result

Notice the comments are left intact and there’s a print statement added to verify mass is correctly stored in mass. Now you run your code—yes, it’s incomplete, but you decide to run it to confirm that the first step is correctly implemented.

Enter mass in kg: 72.1
72.1

So that works as expected. Now you decide you can move on to step 2.

"""
Kinetic Energy Calculator
Egbert Porcupine <egbert.porcupine@uvm.edu>
CS 1210
"""

# Step 1: Prompt user for mass in kg and convert 
#         input to float and save result
mass = float(input('Enter mass in kg: '))
print(mass)
# Step 2: Prompt user for velocity in m / s and 
#         convert input to float save result
velocity = float(input('Enter velocity in m / s: '))
print(velocity)
# Step 3: Calculate kinetic energy in Joules using formula
# Step 4: Display pretty result

Now when you run your code, this is the result:

Enter mass in kg: 97.13
97.13
Enter velocity in m / s: 14.5
14.5

Again, so far so good. Now it’s time to perform the calculation of kinetic energy.

"""
Kinetic Energy Calculator
Egbert Porcupine <egbert.porcupine@uvm.edu>
CS 1210
"""

# Step 1: Prompt user for mass in kg and convert 
#         input to float and save result
mass = float(input('Enter mass in kg: '))
print(mass)
# Step 2: Prompt user for velocity in m / s and 
#         convert input to float save result
velocity = float(input('Enter velocity in m / s: '))
print(velocity)
# Step 3: Calculate kinetic energy in Joules using formula
kinetic_energy = 0.5 * mass * velocity ** 2
print(kinetic_energy)
# Step 4: Display pretty result

You run your code again, testing different values.

Enter mass in kg: 22.7
22.7
Enter velocity in m / s: 30.1
30.1
10283.213500000002

At this point, you decide that getting the input is working OK, so you remove the print statements following mass and velocity. Then you decide to focus on printing a pretty result. You know you want to use format specifiers, but you don’t want to fuss with that quite yet, so you start with something simple (but not very pretty).

"""
Kinetic Energy Calculator
Egbert Porcupine <egbert.porcupine@uvm.edu>
CS 1210
"""

# Step 1: Prompt user for mass in kg and convert 
#         input to float and save result
mass = float(input('Enter mass in kg: '))
# Step 2: Prompt user for velocity in m / s and 
#         convert input to float save result
velocity = float(input('Enter velocity in m / s: '))
# Step 3: Calculate kinetic energy in Joules using formula
kinetic_energy = 0.5 * mass * velocity ** 2
# Step 4: Display pretty result
print(f'Mass = {mass} kg')
print(f'Velocity = {velocity} m / s') 
print(f'Kinetic energy = {energy} Joules')

Now you run this and get an error.

Enter mass in kg: 17.92
Enter velocity in m / s: 25.0
Traceback (most recent call last):
  File "/blah/blah/kinetic_energy.py", line 10, in <module>
    print(f'Kinetic energy = {energy} Joules')
NameError: name 'energy' is not defined

You realize that you typed energy when you should have used kinetic_energy. That’s not hard to fix, and since you know the other code is working OK you don’t need to touch it.

Here’s the fix:

"""
Kinetic Energy Calculator
Egbert Porcupine <egbert.porcupine@uvm.edu>
CS 1210
"""

# Step 1: Prompt user for mass in kg and convert input 
#         to float and save result
mass = float(input('Enter mass in kg: '))
# Step 2: Prompt user for velocity in m / s and convert 
#         input to float save result
velocity = float(input('Enter velocity in m / s: '))
# Step 3: Calculate kinetic energy in Joules using formula
kinetic_energy = 0.5 * mass * velocity ** 2
# Step 4: Display pretty result
print(f'Mass = {mass} kg')
print(f'velocity = {velocity} m / s') 
print(f'kinetic energy = {kinetic_energy} Joules')

Now this runs without error.

Enter mass in kg: 22.901
Enter velocity in m / s: 13.33
Mass = 22.901 kg
Velocity = 13.33 m / s
Kinetic energy = 2034.6267494499998 Joules

The last step is to add format specifiers for pretty printing, but since everything else is working OK, the only thing you need to focus on are the format specifiers. Everything else is working!

"""
Kinetic Energy Calculator
Egbert Porcupine <egbert.porcupine@uvm.edu>
CS 1210
"""

# Step 1: Prompt user for mass in kg and convert input 
#         to float and save result
mass = float(input('Enter mass in kg: '))
# Step 2: Prompt user for velocity in m / s and convert 
#         input to float save result
velocity = float(input('Enter velocity in m / s: '))
# Step 3: Calculate kinetic energy in Joules using formula
kinetic_energy = 0.5 * mass * velocity ** 2
# Step 4: Display pretty result
print(f'Mass = {mass:.3f} kg')
print(f'velocity = {velocity:.3f} m / s') 
print(f'kinetic energy = {kinetic_energy:.3f} Joules')

You test your code:

Enter mass in kg: 100
Enter velocity in m / s: 20
Mass = 100.000 kg
Velocity = 20.000 m / s
Kinetic energy = 20000.000 Joules

You decide that’s OK, but you’d rather have comma separators in your output, so you modify the format specifiers.

"""
Kinetic Energy Calculator
Egbert Porcupine <egbert.porcupine@uvm.edu>
CS 1210
"""

# Step 1: Prompt user for mass in kg and convert input 
#         to float and save result
mass = float(input('Enter mass in kg: '))
# Step 2: Prompt user for velocity in m / s and convert 
#         input to float save result
velocity = float(input('Enter velocity in m / s: '))
# Step 3: Calculate kinetic energy in Joules using formula
kinetic_energy = 0.5 * mass * velocity ** 2
# Step 4: Display pretty result
print(f'Mass = {mass:,.3f} kg')
print(f'velocity = {velocity:,.3f} m / s') 
print(f'kinetic energy = {kinetic_energy:,.3f} Joules')

You test one more time and get a nice output.

Enter mass in kg: 72.1
Enter velocity in m / s: 19.5
Mass = 72.100 kg
Velocity = 19.500 m / s
Kinetic energy = 13,708.012 Joules

Looks great!

Now we can remove the comments we used as scaffolding, and we finish with:

"""
Kinetic Energy Calculator
Egbert Porcupine <egbert.porcupine@uvm.edu>
CS 1210
"""

mass = float(input('Enter mass in kg: '))
velocity = float(input('Enter velocity in m / s: '))
kinetic_energy = 0.5 * mass * velocity ** 2
print(f'Mass = {mass:,.3f} kg')
print(f'velocity = {velocity:,.3f} m / s') 
print(f'kinetic energy = {kinetic_energy:,.3f} Joules')

So now you’ve seen how to do incremental development.2 Notice that we did not try to solve the entire problem all at once. We started with comments as placeholder / reminders, and then built up the program one step at a time, testing along the way. Using this approach can make the whole process easier by decomposing the problem into small, manageable, bite-sized (or should I say “byte-sized”?) chunks. That’s incremental development.

Original author: Clayton Cafiero < [given name] DOT [surname] AT uvm DOT edu >

No generative AI was used in producing this material. This was written the old-fashioned way.

This material is for free use under either the GNU Free Documentation License or the Creative Commons Attribution-ShareAlike 3.0 United States License (take your pick).

Footnotes

  1. Here we’ve excluded if __name__ == __main__: to avoid clutter in presentation.↩︎

  2. If you’re curious about how the pros do iterative and incremental development, see the Wikipedia article on iterative and incremental development: https://en.wikipedia.org/wiki/Iterative_and_incremental_development↩︎