Program Design
In this lecture we will show how a small program is designed, debugged, and improved, using invasion percolation as an example.
Requires: Python
Introduces: Techniques for constructing testable, modifiable programs.
Motivating questions:
- What’s the “best” way to design a program?
- How should the design of my program change as I add to it?
- How can I make my program easier to test?
Lectures:
- Introduction (pdf, ppt)
- The example problem we will be using in these lectures is Inversion Percolation
- Inversion Percolation involves simulating a flow through a porous medium.
- We use a simple 2D grid model where each cell’s value represents its porosity.
- The Grid (pdf, ppt)
- We use sentinel values in our 2D grid to represent cells that have been filled
- Use start with using an array of arrays to represent the 2D grid
- Initialize the values of the 2D grid
- Aliasing (pdf, ppt)
- Manipulating objects (like lists) can be tricky unless you are clear on how variables refer to them
- Two differently named variables can refer to the same object.
- This can lead to bugs if you expect that a change with one variable should not have an effect on another.
- Randomness (pdf, ppt)
- Python’s random package supplies a pseudorandom number generator (PRNG).
- Good PRNG produce a sequence of numbers that have statistics similar to true random numbers
- Using a “seed” value, PRNG follow an algorithm to output a sequence of numbers.
- Neighbors (pdf, ppt)
- At each time step, we need to decide which cell the flow fills.
- Our first attempt at produces code that has a lot of repeated tests, and doesn’t handle edge cases.
- We can use short-circuit evaluation reduce duplication in our tests.
- The general pattern is: if <sanity check> and <test> then…” which only runs the test if the sanity check passes.
- Handling Ties (pdf, ppt)
- Assembly (pdf, ppt)
- Bugs (pdf, ppt)
- Refactoring (pdf, ppt)
- Testing (pdf, ppt)
- Tuning (pdf, ppt)
- Exercises

I tried to track back the posts but I didn’t find why the first episodes in this series are so short… Any reason? Breaks are wonderful, but here I had the impression they are too short…
Nevertheless: I really think this approach of video-lectures is amazingly good.
I agree with Tiago. Great work, but I think 6-10 minute episodes are ideal.
This is the only SC series that is completely unreadable because of the pacing.