2

One normal procedure to implement debugging is the use of

#ifdef DEBUG

...debugging tests...

#endif

Sometimes, a typical "checking" algorithm is needed in some parts of the code, and so one would normally like to wrap it on a function, such that the code is only called when DEBUG flag is defined. Let that function be called myDebug();

I see two natural approaches for this: either ifdef is added on each time myDebug() is called, like this:

#ifdef DEBUG

myDebug();

#endif

Or myDebug is defined as:

void myDebug()
{
#ifdef DEBUG

...code...

#endif
}

Basically, the first avoids the function calling, while the second avoid putting the code with a lot of #ifdef/#endif.

Is there any "standard" convention to choose one of the two, or another way that I don't know? Is is worth to choose the second from the "styling" point of view?

Thanks

Jorge Leitao
  • 19,085
  • 19
  • 85
  • 121
  • It depends. The title talks about the *cost*, but the rest of your question doesn't really focus on that, but just asks which approach is preferable. What are you asking, exactly? Does the precise performance cost of each option matter? – jalf Apr 16 '12 at 09:05
  • Though most compilers will optimize an empty function, I would personally choose the first option.. – Anerudhan Gopal Apr 16 '12 at 09:06
  • Using optimisation, an empty function will probably be ignored – Alex Z Apr 16 '12 at 09:06
  • The empty function *can* be optimized if the compiler has access to it - i.e. it knows its empty. It can or can not happen. – Luchian Grigore Apr 16 '12 at 09:07
  • See [`#ifdef` Considered Harmful](http://www.ethernut.de/pdf/ifdefs.pdf) – Peter Wood Apr 16 '12 at 09:47
  • [NO-OP in c/c++][1] [1]: http://stackoverflow.com/questions/1306611/how-do-i-implement-no-op-macro-or-template-in-c – tuxuday Apr 16 '12 at 10:00
  • See also [C #define macro for debug printing](http://stackoverflow.com/questions/1644868/c-define-macro-for-debug-printing) and [DEBUG macros in C++](http://stackoverflow.com/questions/14251038/debug-macros-in-c) – user Mar 10 '14 at 21:27

1 Answers1

8

I'd go with number 3:

#ifdef DEBUG
#define myDebug(x) myDebug(x)
#else
#define myDebug(x) {}
#endif

This way, there's no pesky #ifdefs everywhere in the code, and for a non-debug build no code will be generated.

Prof. Falken
  • 24,226
  • 19
  • 100
  • 173
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 3
    +1 I would change `#define myDebug(x)` to `#define myDebug(x) {}` to avoid warnings – MByD Apr 16 '12 at 09:07
  • Or better to `#define myDebug(x) {(void)x;}` to dismiss warning `unused variable 'x'` – Agnius Vasiliauskas Apr 16 '12 at 10:10
  • 1
    @Binyamin: `do { } while (false)` should be used rather than `{ }` - see http://stackoverflow.com/questions/154136/why-are-there-sometimes-meaningless-do-while-and-if-else-statements-in-c-c-mac – Tony Delroy Apr 16 '12 at 10:16
  • @TonyDelroy - You're right, I actually remembered something like that, but not exactly.. – MByD Apr 16 '12 at 11:51