Don't Be a Dummy: A Developer's Tale

When you spend enough time working on software development, you sometimes develop an overconfidence that you really shouldn't have. This is a big mistake. I often make the most absurd errors because of it. Take this example from the other day:

val = ...  # (1)
try:
    a = int(val)
except ValueError:
    a = None

Here, the val variable can be an integer, a string that can be converted into an integer, or None (1). I thought, "If it's None, I'll just set it to None; for all other cases, I'll convert it to an integer." After all, I was certain that the int function raises a ValueError if it can't convert something.

Feeling smug about my logic, I didn't even bother running the code before pushing it (this is where I start yelling at myself internally). Later, I realized that int(None) doesn't raise a ValueErrorβ€”it raises a TypeError. My code failed during testing.

Lesson Learned: Test Everything

Even if you're changing something as trivial as a quote in a single line of code, make sure you test it first. Otherwise, you'll end up like me: assuming you're a genius and realizing you were being a dummy all along. πŸ˜…

07/2016