Examples Before Algorithms

If you cannot say what should happen with a concrete example, you are not ready to implement it. Normal, edge and invalid cases for every operation; predict before running; examples become tests; find the bug from the example.

Examples Before Algorithms

Cart = [] → add Laptop → [Laptop × 1] → add Laptop → [Laptop × 2] → add Mouse → [Laptop × 2, Mouse × 1]. Four states and three operations, and the algorithm for addItem is already visible in them. If you cannot explain what should happen with a concrete example, you are probably not ready to implement it.

Q · You know the cart's operations and rules and still cannot see the algorithm. What do you write down so that the algorithm becomes visible?
Normal, Edge, Invalid

For every operation, three examples: the normal case, the edge case, and the invalid one. addItem: a new product; a product already present; quantity 0. The normal case writes the happy path, the edge case finds the branch, the invalid case fixes where the checks go and proves the state survives rejection.

Q · You have one example per operation and the code works for it. Which examples are missing, and how do you find them systematically instead of waiting for bugs?
Predict the State Before Running the Code
▶ lab

Before running cart.add("laptop"); cart.add("laptop"); cart.add("mouse"), write what the cart will contain. Then run it. The gap between predicted and actual is where your model of the code is wrong — and a prediction about state, not just about the outcome, tells you which line.

Q · The code is written. What do you do right before pressing run so that the run teaches you something instead of just reassuring you?
Examples Become Tests

Requirements → examples → expected behaviour → tests. The concept record's tests each name the example they came from: "adding the same product twice increases the quantity" is add-again, encoded. A test that cannot name its example is testing the code's shape, not the cart's behaviour.

Q · The examples exist and the code works for them by hand. How do the examples become tests without the tests becoming a second implementation to maintain?
Predict the Bug
▶ lab

"Every addItem call inserts a new row." Before running it, say what happens when the same product is added twice. Duplicate entries — and the missing line is a rule, not a typo. Reading code for the example it would get wrong is how you review a cart you did not write.

Q · You are given a cart implementation you did not write. How do you find its bug from the examples, before running it — and how do you know the bug is a missing rule rather than a slip?