ZOMDir > Blog

Monday, 23 June 2014

"IF THEN" but what "ELSE"

Recently I discovered a very stupid bug in the code of ZOMDir.com

The code was used to set a variable and was something like this:

if parentStr == "":
  newParentStr = newStr
else:
  # ParentStr isn't empty
  if newStr not in parentStr:
    newParentStr = addStrToParent(newStr, parentStr)

A better look at the code should have learned me that there is something wrong. The second if-statement doesn't have an else statement.

Oops in rare occassions the variable newParentStr isn't set and an Error is raised. 

Luckily I was the first one to find out. Even better, the solution is very simple. The second test isn't necessary at all. It was only required for some early testing. So the code is now:

if parentStr == "":
  newParentStr = newStr
else:
  newParentStr = addStrToParent(newStr, parentStr)

This is a better, isn't it? 

Hans

--
ZOMDir.com is a dynamic directory and a wiki
Everone is able to add a link in 10 seconds
To learn more view this Slideshare presentation

No comments:

Post a Comment