How much sanity checking is required for function inputs?

Michael Selik michael.selik at gmail.com
Wed Apr 20 01:18:23 EDT 2016


On Tue, Apr 19, 2016 at 11:23 PM Christopher Reimer <
christopher_reimer at icloud.com> wrote:

> On 4/19/2016 1:02 AM, Michael Selik wrote:
>
> > Why relocate rather than remove? What message would you provide that's
> > better than ``KeyError: 42`` with a traceback that shows exactly which
> > dictionary is being used and how?
>
> I think you misread my code. No dictionary exception occurs in the
> sanity checks. Below is the full function with the revised sanity check
> for positions that compares the input list with the two valid lists of
> board positions.
>

Perhaps I did misread it. What is the purpose of the "sanity check"? If
it's not obvious I suggest revising the code rather than adding comments.

The first time I read your code, I thought the check was designed to avoid
the possibility of a KeyError a few lines later. My suggestion was to
simply allow bad inputs to cause KeyErrors and not clutter your code. This
second time I'm reading, it seems to be a sort of boundary check, but using
``in`` which would cause a bug... Did you mean to use an inequality?


> > I meant, what goes wrong if the number of positions input is other than
> > 16? Without these "sanity" checks, your functions might be reusable in,
> > say, a checkers game.
>
> I have no desire to write reusable code for two
> similar but different games at the same time.
>

Reusability is a nice side-effect of fewer "sanity checks". Other goals are
clarity, efficiency, and productivity.



More information about the Python-list mailing list