I’ve been programming computers long enough that I shouldn’t have written the bug that one of my clients found today. It’s embarrassing not only because my client saw it, but also because the mistakes that caused the bug are things that I’ve done before and learned from. But then I got in a hurry.
I made three mistakes:
- I used cut-and-paste to duplicate some code.
- I used negative logic in an if-then-else statement.
- It was “just a little change,” so I didn’t test the program thoroughly after making the change.
Of the three, the last is the dumbest and most embarrassing. When my client first told me of the error I couldn’t believe it. How could I have made such an amateurish mistake? Cut-and-paste in itself isn’t a sin, but all too often you end up duplicating code and creating a maintenance nightmare because some time down the road you’ll end up having to make almost identical changes to multiple pieces of almost identical code. And the last…negative logic? Almost always a bad idea. For example, you can write:
if some condition is true then
do this
else
do that
Simple, yes? It’s also perfectly valid to write this:
if not (some condition is true) then
do that
else
do this
Don’t do it! When you come back to that code in maintenance mode six months later, you’ll likely overlook the “not” and end up coding some of “this” where “that” is supposed to go. Simple rule: if there’s an else, use positive logic in the conditional.
As I said, I learned all of these lessons a long time ago. I guess it just shows that no matter how long you’ve been doing something, you have to pay attention or you get sloppy. Sheesh.