Silly Mistakes
Have you ever been in the middle of writing a function, and a bright idea hit you like a piano out of mid air, or suddenly interrupted by a flash mob? Me, too. What were we talking about?
This is among the most universal experiences in programming: distractions that lead to mistakes. However, I'm not really concerned with the glaring or subtle bugs that may occur as a result. Instead, I'd like to point out a category of mistakes that are nothing if not just plain silly.
Here's an example of a mistake I see all the time:
public bool Foo(string input)
{
try
{
DoSomethingExtremelyImportant(input);
return true;
}
catch (Exception ex)
{
throw ex;
}
return true;
}
Can you spot it? Sure, because you didn't write it and it's staring back you. The function returns a boolean value [presumably] to indicate success or failure, but for some reason it never returns false
. Instead of returning false
, it rethrows the exception.
I came across code like this in an inherited code base. To the author's credit, this was not a pervasive pattern. They, like all of us at one time or another, succumbed to a distraction of some sort, and something weird happened. They wrote code that doesn't make any sense. It's not a huge deal, and is easily corrected.
These things are more likely to happen when you work by yourself. To avoid silly mistakes like this, share your work with others. Seek code reviews, pair programming sessions, and remember to go back and read your own code.