Friday, March 30, 2007

The evil #if DEBUG

I recently received a bug report for an ASP.NET site I've been maintaining but when I went off to test out the problem running the site through Visual Studio everything worked fine. Slightly confused, I went back to the live site and immediately reproduced the problem. So I thought the problem must be caused by the site not being in sync with the the development box so I uploaded the latest code. The problem was still occurring... I was fairly unfamiliar with the code so I started stepping through it. You've no doubt guessed the problem by now from the title of this post. There was some code wrapped in an #if DEBUG directive that actually overrode the parameters passed into the page via a query string. I removed that and the problem on the live site was reproducible in Visual Studio. But I wasted an hour or so getting to the bottom of it.

So the moral of the story is don't change behaviour of your code with #if DEBUG statements. The only use I've found for #if DEBUG is to log things, and generally that can be avoided through the use of the trace listener infrastructure. 

No comments: