In a few places in our code we use #if DEBUG blocks to simplify development. Things like:
#if DEBUG
serverIP = localhost;
#else
serverIP = GetSetting()
#endif
or
private bool isLicensed()
#if DEBUG
return true;
#endif
return CheckSetting()
There are also a few places where we make cosmetic changes like this:
#if DEBUG
background = humorousImage.jpg
#else
background = standardColor
#endif
Is it dangerous to depend on #if debug to make development easier? If it is, what is a valid use of #if debug?
The problem with doing this is that it makes you much less likely to find bugs in the #else
.
In general, your debug builds should be as similar as possible to your release builds.
Ideally, I think you would move these settings to configuration files and keep the #IF Debug directives for testing, logging, and additional "debugging" tasks. Also, keep in mind that customer facing code that you ever needed to provide a "debug" build for would now behave entirely different. My two cents.
It is a really bad idea. If you're trying to catch a production bug your debug clauses will certainly trip you up at some stage or another. You want to be as close as possible to code that runs in production. In your example you'll never be able to find a bug in CheckSetting()
By the looks of things your code is too tightly coupled. What you want to be doing is to make modules/classes less dependent on each-other and practice Test Driven Development. Also have a look at Inversion of Control (aka Dependency Injection).
Working Effectively with Legacy Code has some useful insights on how to introduce TDD. It also has some really good pointers on how to do TDD in various scenarios where it might be hard to test things.
I can't say this is something I'm keen on doing, personally - I work in an environment where our main application (deployed to 400+ users) has 60+ modules - and with 5 developers working on the projects and releasing modules, you just know sooner or later someone will accidentally release a debug module. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With