Fixing Bugs Without Affecting Production Code In C#

In the world of programming, a 100% bug free application is almost taboo to say. Just when you think you’ve fixed the final bug, chances are, you’ve opened up a new unexpected bug. While you’re debugging you generally only want to look at a small section of your over-all data. Using a full dataset to debug is very time consuming and largely wasted, but how do you debug specific data while still ensuring that you keep the functionality on live without needing to change any of your code. Well there is a simple answer to this. The only caveat to this solution is that you must always debug in DEBUG mode in your configuration manager, and must always push code to production in RELEASE mode (which is just good practice to begin with.)

Where to change your mode in configuration manager

I present to you
“The #if directive”.



Everything you need to know. The concept should be pretty straight forward to even the newest of programmers. Visual Studio even makes using #if directives extremely easy to read in code.

As you can see, when I have my project in RELEASE mode. Any code that is contained within #if DEBUG is greyed out, it’s actually even completely ignored by the compiler altogether!

Beautiful, isn’t it?

There are a few other options at your disposal. Another consideration is setting a mode in your app.config. The benefit to this is that in your local and production environments you’d never need to change any value once they’re originally set. The downside to this is that code readability is a bit higher since an app.config value won’t edit the view on the editor the way #if directives do.

#if directives are an easy to use, easy to manage, and easy to read way to ensure that DEBUG testing clauses don’t get leaked into your live, working, production environment. Never again will you need to remember to undo all your testing code. It can co-exist peacefully with your production code, ready for the next bug. It may make your code files slightly bigger, but I’d say it’s very worth it for the maintainability and streamlining from local to production.