Mr. Darto, who runs the parking lot at the market near Mrs. Sri's stall, asks for a program that calculates the motorbike parking fee. The sign at his booth says:
Motorbike parking: first 2 hours $3. Each hour after that $2. At most $15 a day.
Dodi, Mrs. Sri's nephew, opens the editor right away and types fee ← hours * 2. He tries it for 1 hour. The result is 2, but it should be 3. He patches the program with an IF, a quick fix for that one case. Now 1 hour is right, but 3 hours is wrong. He patches it again, and then 10 hours gives 19, although the highest fee is 15. Each patch fixes one case and breaks another.
Dodi's problem is not the way he writes pseudocode. The problem is that he wrote code before he knew exactly what the steps were. This lesson is about the work that comes before the code: breaking the problem down, finding the pattern, leaving out what does not matter, then testing at the edges.
Steps first, code later
First write the steps in plain sentences, as if you were explaining them to someone:
- Read how many hours the bike was parked.
- Calculate the fee.
- Print the fee.
Steps 1 and 3 are already easy to write: READ hours and PRINT. Step 2 is still too big to write as code right away, so break it down into smaller steps:
- Start from 3, the price of the first two hours.
- If the bike was parked for more than 2 hours, add 2 for each hour after the second hour.
- If the result is more than 15, make it 15.
Keep breaking the steps down until each one is small enough to be written as one or two lines of pseudocode. This is called decomposition: one big problem is split into a few small problems that are each easy to solve.
Find the pattern
"Each hour after that" is still a sentence, not a formula yet. To find the formula, first calculate a few examples by hand:
| Hours | Fee | How it's worked out |
|---|---|---|
| 1 | 3 | still the first two hours |
| 2 | 3 | still the first two hours |
| 3 | 5 | 3 + 1 × 2 |
| 4 | 7 | 3 + 2 × 2 |
| 5 | 9 | 3 + 3 × 2 |
The pattern shows in the last column: the number multiplied by 2 is the number of hours after the second hour, which is hours - 2. So for more than 2 hours of parking, the formula is 3 + (hours - 2) * 2. Before you use the formula, check it with a program. The result is below the code:
FOR hours ← 3 TO 6
PRINT hours, "h:", 3 + (hours - 2) * 2
END FOR
3 h: 5
4 h: 7
5 h: 9
6 h: 11
The results for 3, 4 and 5 hours match the table. Once you have found the pattern, one formula can replace many IFs.
Leave out what doesn't matter
A parked bike has a number plate, a colour, a brand and an owner. None of that is used to calculate the fee. The calculation needs only one number: how many hours the bike was parked. Choosing the data that matters and leaving out the rest is called abstraction.
Abstraction also means giving a calculation a name. Write the fee calculation as one function, bikeFee. Then the main program only has to call the function's name:
FUNCTION bikeFee(hours)
fee ← 3
IF hours > 2 THEN
fee ← fee + (hours - 2) * 2
END IF
IF fee > 15 THEN
fee ← 15
END IF
RETURN fee
END FUNCTION
PRINT "Parked 4 hours, pay", bikeFee(4)
Parked 4 hours, pay 7
Someone who reads bikeFee(4) does not need to know what is inside the function to understand what it means. It is the same as Mrs. Sri in the Functions lesson, who does not need to know what is inside her blender to make juice.
The plan becomes comments
Replace step 2 of the first list with its three smaller steps, and you have a plan of five steps. Do not throw the plan away once the code is done. Write each step as a comment, which is a line that starts with // and that the computer skips. Then fill in the code below each comment, one at a time. With the input 4:
// 1. Read how long it was parked, in hours.
READ hours
// 2. The first two hours cost 3.
fee ← 3
// 3. Add 2 for each hour after the second.
IF hours > 2 THEN
fee ← fee + (hours - 2) * 2
END IF
// 4. At most 15.
IF fee > 15 THEN
fee ← 15
END IF
// 5. Print the fee.
PRINT "To pay:", fee
To pay: 7
If a result is wrong later, the comments show which lines do which step.
Test at the edges
A program that is right for 4 hours is not always right for every number of hours. Bugs most often appear at an edge, which is a point where the rule changes. This parking fee has two edges:
- Between 2 and 3 hours. From the third hour, the extra 2 an hour is added.
- Between 8 and 9 hours. For 8 hours the fee is 3 + 6 × 2, exactly 15. From 9 hours, the highest fee applies.
This program tests the function bikeFee at both edges:
FUNCTION bikeFee(hours)
fee ← 3
IF hours > 2 THEN
fee ← fee + (hours - 2) * 2
END IF
IF fee > 15 THEN
fee ← 15
END IF
RETURN fee
END FUNCTION
FOR EACH hours IN [1, 2, 3, 8, 9]
PRINT hours, "h:", bikeFee(hours)
END FOR
1 h: 3
2 h: 3
3 h: 5
8 h: 15
9 h: 15
Those five numbers of hours were chosen on purpose: 1 is inside the first rule, 2 and 3 are on the two sides of the first edge, and 8 and 9 on the two sides of the second. Dodi's first program was already wrong for 1 hour. The exercise also runs your program at the edges of its rule.
⚠️ Edges that go wrong
- A number copied from the sign together with its dollar sign.
fee ← $3stops with the message "Line 1: the symbol$is not understood." In code, write3with no dollar sign. - Forgotten brackets.
fee ← 3 + hours - 2 * 2is calculated as3 + hours - 4, because multiplying is done first. For 4 hours the program printsTo pay: 3instead of 7, with no error message. Write3 + (hours - 2) * 2. - A
feethat is only set inside anIF. Iffee ← 3 + (hours - 2) * 2is only insideIF hours > 2, parking for 1 hour never setsfee. The program stops with the message "Line 5:feeis used before it has a value." Writefee ← 3first, above theIF. - Steps in the wrong order. If the 15 limit is checked before the extra hours are added, parking for 9 hours prints
To pay: 17. Step 4 (the 15 limit) has to come after step 3 (the extra hours), exactly as in the plan written as comments. - A missing
END IF. The program stops with the message "Line 3: this IF is never closed." The message also says where to close it: "Add END IF after line 4."
TL;DR: Before you write code, write the steps in plain sentences. Break down any step that is still big, until each step can be written in one or two lines. This is called decomposition. Calculate a few examples by hand to find the pattern, which is a formula that fits all of those examples. Use only the data the calculation needs, and write the calculation as a function so it has a name. This is called abstraction. Keep your steps as comments (lines starting with // that the computer skips) above the code. Last, test your program with the values just before and just after each edge, which is a point where the rule changes.
⚡ Steps first, code later
Meaning: The example in this lesson is a program for a motorbike parking fee: the first 2 hours cost $3, each hour after that costs $2, and the highest fee is $15 a day. Before you write code, first write the steps in plain sentences: read how many hours, calculate the fee, print the fee. A step that is still too big, like "calculate the fee", is broken down again until each step fits in one or two lines of pseudocode. This is called decomposition: one big problem is split into a few small problems that are each easy to solve.
⚡ Find the pattern
Write:
FOR hours ← 3 TO 6
PRINT hours, "h:", 3 + (hours - 2) * 2
END FOR
Guess first: if you forget the brackets, the formula becomes 3 + hours - 2 * 2. What is the result for 4 hours of parking?
3 instead of 7, with no error message. Multiplying is done first, so the calculation is 3 + 4 - 4.
Meaning: To find the formula for the fee, first calculate a few examples by hand: 3 hours 5, 4 hours 7, 5 hours 9. The pattern: each hour after the second hour adds 2, and the number of those hours is hours - 2. So the formula is 3 + (hours - 2) * 2. The program above prints what the formula gives for 3 to 6 hours, to compare with your own calculations. One formula replaces many IFs.
⚡ Leave out what doesn't matter
Meaning: A motorbike has a number plate, a colour and a brand, but the fee calculation needs only one number: how many hours the bike was parked. Choosing the data that matters and leaving out the rest is called abstraction. Abstraction also means giving a calculation a name: write the fee calculation as a function, bikeFee(hours). Someone who reads bikeFee(4) does not need to know what is inside the function to understand what it means.
⚡ Test at the edges
Guess first: the fee rule changes after 2 hours, and the fee stops rising at 15 after 8 hours. Which numbers of hours most need to be tested?
1, 2, 3, 8 and 9 hours. 1 is inside the first rule. 2 and 3 are on the two sides of the first edge, and 8 and 9 are on the two sides of the second edge.
Meaning: A program that is right for 4 hours is not always right for every number of hours. Bugs most often appear at an edge, which is a point where the rule changes. So test your program on both sides of each edge.