Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a bad idea to put development shortcuts in #if DEBUG blocks?

Tags:

c#

debugging

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?

like image 876
Bobwise Avatar asked Jul 14 '10 20:07

Bobwise


4 Answers

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.

like image 197
SLaks Avatar answered Oct 01 '22 20:10

SLaks


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.

like image 35
Flesrouy Avatar answered Oct 01 '22 20:10

Flesrouy


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.

like image 20
leonm Avatar answered Oct 01 '22 20:10

leonm


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. :)

like image 33
Will A Avatar answered Oct 01 '22 20:10

Will A