Defensive Programming

When we program, we often miss certain important aspects, and introduce potential errors in the programs, that may manifest only for certain inputs. For example, it is estimated that even in mature software, it is common to find at least one bug in every thousand lines of code. Defensive programming is a term used to describe a collection of techniques that reduce the chances of errors (also called bugs) escaping into the program.

Two very common methods for defensive programming are:

Set yourself up for easy testing and debugging

When are you ready to test?

Classes of tests:

Testing approaches:

Blackbox testing

def sqrt(x, eps):
  """ Assumes x and eps are non-negative floats
      Returns res such that x - eps <= res*res <= x+eps"""