Up: Lectures

Program Design

September 19th, 2011 Leave a comment Go to comments

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:

  1. What’s the “best” way to design a program?
  2. How should the design of my program change as I add to it?
  3. How can I make my program easier to test?

Lectures:

  1. 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.
  2. 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
  3. 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.
  4. 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.
  5. 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.
  6. Handling Ties (pdf, ppt)
  7. Assembly (pdf, ppt)
  8. Bugs (pdf, ppt)
  9. Refactoring (pdf, ppt)
  10. Testing (pdf, ppt)
  11. Tuning (pdf, ppt)
  12. Exercises

  1. Tiago Rodrigues
    August 12th, 2010 at 12:20 | #1

    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.

  2. December 11th, 2010 at 17:42 | #2

    I agree with Tiago. Great work, but I think 6-10 minute episodes are ideal.

  3. tissit
    November 3rd, 2011 at 07:43 | #3

    This is the only SC series that is completely unreadable because of the pacing.