Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is leaving debug sections in the source code a good practise? [closed]

Tags:

c++

c

debugging

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.

like image 360
Ceph Avatar asked Sep 03 '20 09:09

Ceph


People also ask

Should you leave debug statements in code?

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.

Why do we need to debug the program that we are coding?

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.

What is debugging information?

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.

Should debug code be removed from production software?

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:

Should debug code be compiled or just leave it in debug?

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.

Should debug print statements be taken out of production logs?

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.


Video Answer


1 Answers

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 */); 
}
like image 124
Jan Schultke Avatar answered Oct 30 '22 00:10

Jan Schultke