I am working together with a senior C++ developer. In most of his code he includes debug sections. It looks like this:
void do_something() {
int var = 10
#ifndef NODEBUG
trace("Function Start %s", __FUNCTION__);
#endif
while (someCondition) {
var += 1
#ifndef NODEBUG
trace ("Var = %d \n", var)
#endif
}
... (Do some other stuff)
... (Do some more stuff)
#ifndef NODEBUG
trace("Function End %s", __FUNCTION__);
#endif
}
I am not as experienced as he is, but these code snippets are all over the code and this really clogs the readability and the understandability. In my oppinion the preprocessor conditions should AT LEAST be properly intended with the rest of the code.
Is this practise something that I should form a habit of? I am not sure if this a good way but in my opinion this immensely bloats the code and should be left out.
Debug print statements should be taken out; however, if you're needing to add them to debug a production problem then it may be worth considering if you have enough information being put into your logging framework.
Definition: Debugging is the process of detecting and removing of existing and potential errors (also called as 'bugs') in a software code that can cause it to behave unexpectedly or crash. To prevent incorrect operation of a software or system, debugging is used to find and resolve bugs or defects.
The answer is - debugging information. Debugging information is generated by the compiler together with the machine code. It is a representation of the relationship between the executable program and the original source code. This information is encoded into a pre-defined format and stored alongside the machine code.
Code added specifically for debugging should be removed from production software. Whether this is complete removal or being put into conditional compilation sections (as in C/C++/C#) is up to you and your coding standard. There are a number of reasons for this:
If there are times when you might need the debug output then leaving it in and compiled but putting it's execution in conditional statements controlled by configuration would be an acceptable compromise. For most of the time the code isn't executed but can be at the flip of a switch.
Show activity on this post. Debug print statements should be taken out; however, if you're needing to add them to debug a production problem then it may be worth considering if you have enough information being put into your logging framework.
It's not generally a bad practice to add code within debug sections. As long as this code doesn't fundamentally change the behavior of your function, it doesn't add much complexity and can give you useful information. However, it does decrease readability.
It is very rare that you really need a dedicated code section that only runs on debug builds. What I very commonly do myself instead is create specialized macros that only perform an action on debug builds. This ends up being much more concise and readable. For example:
// defined in some utility header
#ifndef NODEBUG
#define DEBUG_TRACE(...) trace(__VA_ARGS__)
#else
#define DEBUG_TRACE(...)
#endif
void do_something() {
int var = 10
DEBUG_TRACE("Function Start %s", __FUNCTION__);
while (someCondition) {
var += 1
DEBUG_TRACE("Var = %d \n", var);
}
// ... (Do some other stuff)
// ... (Do some more stuff)
DEBUG_TRACE("Function End %s", __FUNCTION__);
}
If you were to add a #ifndef NODEBUG
code section now, it would become much more obvious that you are changing the behavior of the function instead of just logging something.
This practice can be applied not only to logging but to assertions and other things which only compile on debug builds. If you do this a lot, then the following macros can be of help too:
#ifndef NODEBUG
#define IF_DEBUG_ELSE(expr, alt) expr
#define IF_DEBUG(...) __VA_ARGS__
#else
#define IF_DEBUG_ELSE(expr, alt) alt
#define IF_DEBUG(...)
#endif
void example(int x) {
IF_DEBUG_ELSE(/* action on debug builds */, /* action on release builds */);
IF_DEBUG(/* one-liner action only on debug builds */);
}
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