Wednesday, July 1, 2009

Compiler Warnings

While working on a project with a medium-to-largish codebase (>10000 lines) I've run across a slew of compiler warnings. Most of these warnings are due to the following practice that I'm going to call an anti-pattern though it should probably be called a mini-anti-pattern:

try
{
...
}
catch (Exception ex)
{
// execute code that never references ex
}

Usually the unused exception ex is there to help during debugging. I do it all the time. But once you're no longer debugging that particular block, a simple // does the trick; makes it obvious to the next person that you were debugging here and makes it easy to re-use should you need to debug there again.

In this case, there were more than 60 compiler warnings streaming across the output screen due to this mini-anti-pattern. So what? Unfortunately that stream was masking a "detected unreachable code" warning. This was bad because not only had I assumed the code was reachable another process also assumed the code was reachable (and being reached!).

On the bright side, at least this provides extra motivation to clean up compiler warnings whenever it's feasible.

No comments :

Post a Comment