Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #107197 > unrolled thread

How much sanity checking is required for function inputs?

Started byChristopher Reimer <christopher_reimer@icloud.com>
First post2016-04-17 12:34 -0700
Last post2016-04-17 12:34 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  How much sanity checking is required for function inputs? Christopher Reimer <christopher_reimer@icloud.com> - 2016-04-17 12:34 -0700

#107197 — How much sanity checking is required for function inputs?

FromChristopher Reimer <christopher_reimer@icloud.com>
Date2016-04-17 12:34 -0700
SubjectHow much sanity checking is required for function inputs?
Message-ID<mailman.115.1460925247.6324.python-list@python.org>
Greetings,

I'm currently building a chess engine to learn the finer details of 
Python. When I learned all flavors of Java in community college a decade 
ago, we had to sanity check the hell out of the input values for every 
function and wrote a lot of redundant code in addition to the 
getters/setters code.

Here's the input sanity checking I got for a generator function to 
return a set of chess pieces for a specified color and position.


def generate_set(color, positions):

     if color not in [VARS['COLOR_BLACK'], VARS['COLOR_WHITE']]:
         raise Exception("Require \'{}\' or \'{}\' for input value, got 
\'{}\' instead.".format(VARS['COLOR_BLACK'], VARS['COLOR_WHITE'], color))

     if len(positions) != 16:
         raise Exception("Require 16 positions for input value, got {} 
instead.".format(len(positions)))


The two sanity checks are fairly straight forward. Color input has to 
match the color constant strings. Positions input has to have 16 items.

I *could* sanity check the positions input to determine if it had a list 
of 16 valid coordinates. The reason I don't is because the code that 
calls this function builds the coordinates from constants with valid 
coordinates. However, if I was doing this in my Java classes, one of my 
instructors rap me on the knuckles for not sanity checking to nth degree.

How much sanity checking is too much in Python?

Thank you,

Chris R

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web