Do you prefer #ifdef _DEBUG
or #ifndef NDEBUG
to specify debug sections of code? Is there a better way to do it? e.g. #define MY_DEBUG
.
I think _DEBUG
is Visual Studio specific, is NDEBUG standard?
Do you prefer #ifdef _DEBUG
or #ifndef NDEBUG
to specify debug sections of code? Is there a better way to do it? e.g. #define MY_DEBUG
.
I think _DEBUG
is Visual Studio specific, is NDEBUG standard?
Be consistent and it doesn't matter which one. Also if for some reason you must interop with another program or tool using a certain DEBUG identifier it's easy to do
#ifdef THEIRDEBUG
#define MYDEBUG
#endif //and vice-versa
Visual Studio defines _DEBUG
when you specify the /MTd
or /MDd
option, NDEBUG
enables standard-C assertions. Use them when apropriate, ie _DEBUG
if you want your debugging code to be consistent with the MS CRT debugging techniques and NDEBUG
if you want to be consistent with assert()
.
If you define your own debugging macros (and you don't hack the compiler or C runtime), avoid starting names with an underscore, as these are reserved.
The macro NDEBUG controls whether assert() statements are active or not.
In my view, that is separate from any other debugging - so I use something other than NDEBUG to control debugging information in the program. What I use varies, depending on the framework I'm working with; different systems have different enabling macros, and I use whatever is appropriate.
If there is no framework, I'd use a name without a leading underscore; those tend to be reserved to 'the implementation' and I try to avoid problems with name collsions - doubly so when the name is a macro.