How much sanity checking is required for function inputs?

Christopher Reimer christopher_reimer at icloud.com
Sun Apr 17 15:34:05 EDT 2016


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





More information about the Python-list mailing list