1

I have an habit to use code like this for debugging blocks that aren't very trivial to write:

if(0) 
{ 
    // debugging code 
}

The problem is "warning C4127: conditional expression is constant".

I am keeping comments for old code that can be deleted any time in the future (but should be kept for a while to have the possibility to follow the intentions of the original writer). I could use "#if 0", but I fail to see any improvement in readability, by contrary (but this could be a matter of taste).

Which would be the friendlier, more readable, warning free solution ?

(Since this is more a question about style, maybe there is a better StackExchange place for it)

EDIT the debugging code is not equivalent to the debugging configuration (_DEBUG): generally is within _DEBUG, but I don't want to pollute it so I have to disable it with if (0)

Liviu
  • 1,859
  • 2
  • 22
  • 48

4 Answers4

5

Use pre-processor:

#if 0 
    // Deactivated code.
#endif

You may also define a macro DEBUG (or name of your choice) and then do something like

#ifdef DEBUG 
    // Debug code.
#endif

Note: I suggest to use your own macro instead of existing ones (as _DEBUG or NDEBUG see _DEBUG vs NDEBUG).

Community
  • 1
  • 1
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • 1
    In addition to avoiding the compiler warning, excluding the conditional code via the preprocessor speeds up compilation, since the compiler doesn't see the disabled code at all. And you're not relying on the optimizer to figure out that it can be eliminated, so you'll probably get a smaller executable even in an unoptimized build. – Wyzard Dec 05 '14 at 15:11
  • 1
    @Wyzard This kind of optimization is nothing compared to readability. – Liviu Dec 05 '14 at 15:17
  • That DEBUG is too close to _DEBUG, but it is better than "John" nevertheless (some like to use their own name), so "#if 0" is good enough, I guess (one doesn't write more since `if (0)` generally requires also the block `{}`) – Liviu Dec 05 '14 at 15:21
  • @Wyzard: Most IDEs gray out deactivated region. – Jarod42 Dec 05 '14 at 15:48
  • NDEBUG is a great discovery (for some) since yesterday ... but what if I want to debug the release configuration? The answer was good enough until Simon Warta definitely changed its meaning. – Liviu Dec 06 '14 at 22:51
  • Sorry, I cannot approve such an answer, NDEBUG is wrong there, is worst that "John" or "Simon". I wonder who could have approved this edit. – Liviu Dec 06 '14 at 22:55
  • Revert back the edit. I strongly suggest your own macro: `_DEBUG` is msvc specific, `NDEBUG` is for `assert`. – Jarod42 Dec 08 '14 at 08:04
3

You can use comma operator (http://en.wikipedia.org/wiki/Comma_operator):

if (0,0) {
  // ...
}

under VS2013 it removes for me this warning, under highest warning level

[edit]

in my code I sometimes use following idiom:

#if defined(_DEBUG)
    static bool bVerbose = false;
    if ( bVerbose ) {
       // do some debugging code, like print to log some data structure etc.
    }
#endif

then when I step through code and I spot place which requires more investigation I change bVerbose in debugger to true and I can see its output, etc.. This way I dont have to rebuild project to enable debugging code, and also try to reproduce problem. Changing of execution point is also very usefull here.

marcinj
  • 48,511
  • 9
  • 79
  • 100
1

I like to do this:

bool test = false;
if ( test )
{
   ...
}

This has the added advantage that I can set a breakpoint on the if statement and change the value of test while the application runs to enable or disable the debug code.

Paul Mitchell
  • 3,241
  • 1
  • 19
  • 22
  • The side effect is the adding of another variable (another type of pollution). Plus, I find it less readable - suppose that you forget it on 'true', someone later might think that the code is necessary (for the errors log, by exemple), while `if (1)` / `if (0)` is less confusing. – Liviu Dec 05 '14 at 15:16
0

Take this! Define and call a lamda function

Con:

  • C++11 only

Pro

  • Constant but the compiler doesn't get it (at least g++)
  • One liner
  • No state in other scopes
  • Intuitive and easy to remember ;)

test code

#include <iostream>

#define ON ([] () { return true; }())
#define OFF ([] () { return false; }())

int main()
{
    if (ON)
    {
        std::cout << "Block 1" << std::endl;
    }

    if (OFF)
    {
        std::cout << "Block 2" << std::endl;
    }

    return 0;
}

Edit: What was meant as a joke in the first place might be fairly handy using the right macros. See above.

Simon Warta
  • 10,850
  • 5
  • 40
  • 78