14 Defensive Programming
12 Apr 2016Note: This lesson draws heavily from, and in some parts quotes directly, the Python Testing lesson developed by Kathryn Huff.
Untested code is broken code. Doing science with untested code is akin to using an experimental device that is uncalibrated, which is generally a bad idea. The best way to write code that works and keeps on working is to assume it's broken, and to build yourself some alarms for when its behavior is outside of what is expected.
This mindset is often called defensive programming.
In this lesson, we will learn about various flavors of testing code, including:
and we will write some of our own, too.
Class material
See the refined version of the live notebook used in class for the detailed lesson: Defensive_Programming.ipynb
Additional resources
- Python Testing by Kathryn Huff
- Effective Computation in Physics, Chapter 18, A. Scopatz and K. Huff. O'Reilly Media. (2015)