View Comments

Recently I've been doing a lot of python scripting. It's a bit of an alien language to me as I'm used to strong types, but maybe not quite as alien as F#! Today I ran into an impossible to find bug. All my return values were as expected, nothing was out of place; yet the test continued to sometimes fail. After borrowing the brain of one of my coworkers I realized what was wrong and I feel very dumb.


Let's look at this code sample:

def compare_function(file1,file2):
	if file1 is None or file2 is None:
		raise Exception('file1 anf file2 must not be None')
	result = execute_util(['compare_images.exe', file1, file2])
	if not result:
		return False
		return float(result.replace("'", '').replace('\n','').strip())

 Do you see a possible problem? Lets suppose I try to compare the result of this function with 0.0%, well what's the difference with that and False? I was expecting to return False on error or return a floating point value if it were successful, but it would be possible to return 0.0 on success. I should have realized this with enough C/C++ experience, but now I feel like such a beginner again. The solution was to return None on error instead of False.