GoalDescribe iterative development and follow a program through three versions, testing each one.
New words
iterative development · building a program in small versions: write a little, test it, then change and improve itversion · one stage of a program, saved before the next change is madetest · to run a program with chosen data to see whether it gives the results you expect
Explanation
Experts rarely write a whole program and then test it once. They use iterative development. First they write a small version that does one job. Next they test it. If it is wrong they fix it. If it is right they add the next feature, and test again. This repeats until the program does everything it should. Each version is a working step. A mistake is easy to find because only a little has changed since the last test. If the whole program is written first, a mistake could be hiding anywhere. A beginner’s first version may be very small, and that is fine.
Version 1 of a ticket program for a school play: it only multiplies. Test: typing 3 gives 24, which is correct (3 × 8). The programmer is happy and moves on.
Python
1tickets = int(input("Tickets? "))
2total = tickets * 83if tickets >= 5:
4 total = total - 55print(total)
Output
35
Version 2 adds a rule: five or more tickets get 5 off. It is tested again with 3 (no discount, 24) and with 5. 5 × 8 = 40, minus 5 is 35, which is correct.
Version
What was added
Test data
Result
1
price for tickets
3
24, correct
2
5 off for 5 or more tickets
5
35, correct
3
a clear message
4
Total price: 32, correct
Version 3 improves the message. Each version was tested before the next one was started.
Class activity
Paper aeroplanes (no computer). Fold a simple paper aeroplane and fly it from a line on the floor. Measure the distance with a ruler or in footsteps. Change only one thing, such as the wing size or a nose fold, and fly it again. Record each version in a table: version, what changed, distance. Make three versions. Which change helped most? Why is changing only one thing at a time useful? Compare this with writing a program in versions. If you have a computer or tablet, ask an adult to help you run version 1 of the ticket program.
Practice
1
Put these steps of iterative development in a sensible order: test the version, write a small first version, add the next feature, fix any mistakes.
Write a small first version; test the version; fix any mistakes; add the next feature (and then test again). The steps repeat.
2
Version 1 of a program reads a number of cups and prints cups * 3. Write version 2: it adds a charge of 2 for delivery when the number of cups is more than 0. What is the output for 4 cups?
Python
1cups = int(input("Cups? "))
2print(cups * 3)
Model answer (lines are separated by /, and [indent] means four spaces at the start of the line): cups = int(input("Cups? ")) / total = cups * 3 / if cups > 0: / [indent] total = total + 2 / print(total). Note: With 4 cups, the output is 14.
3
A test of version 2 is shown. Version 1 passed its test earlier. In which version was the mistake added, and what is wrong? Fix it.
Version
Test data
Expected
Actual
1
3
24
24
2 (uses tickets > 5)
5
35
40
The mistake is in version 2. The line uses tickets > 5, so exactly 5 tickets get no discount, but the plan says 5 or more. Change it to tickets >= 5.
4
Explain why writing and testing a small version at a time makes mistakes easier to find than writing the whole program first.
When only a little has changed since the last test, the mistake must be in that part. In a whole new program, the mistake could be anywhere, and it may be hidden by other mistakes.