How much sanity checking is required for function inputs?

Ethan Furman ethan at stoneleaf.us
Mon Apr 18 01:26:21 EDT 2016


On 04/17/2016 12:34 PM, Christopher Reimer wrote:

> How much sanity checking is too much in Python?

What happens if your extensive sanity checks turn up a bug?

In Python the usual answer is you raise an error:

     raise ValueError('blahblah not a valid color')

What happens if you don't sanity check anything and the wrong color is 
passed in?

Python will raise an error for you:

Traceback
   ...
KeyError: 'blahblah' not found

---

I sanity check for three reasons:

1) raise a nicer error message

2) keep the point of failure close to the error

3) the consequences of bad data are Bad Bad Things (tm)

If none of those apply, I don't bother sanity checking.

--
~Ethan~




More information about the Python-list mailing list