Simple ways to disable parts of code

后端 未结 14 1134
误落风尘
误落风尘 2021-01-31 08:59

This isn\'t a typical question to solve a specific problem, it\'s rather a brain exercise, but I wonder if anyone has a solution.

In development we often need to disable

相关标签:
14条回答
  • 2021-01-31 09:15

    Sometimes I use the below trick to switch between two lazy comments.

    //* <-- remove the first slash
    [code block 1]
    /*/
    [code block 2]
    //*/
    
    0 讨论(0)
  • 2021-01-31 09:15

    the following logic should contain the simplest approach

    if(isMode1)
    {
        //Code for mode1
    }
    else
    {
        //Code for other modes
    }
    

    although I think the correct way is to use PreProcessor Directives

    0 讨论(0)
  • 2021-01-31 09:16

    Sometimes i use this approach to just not overbloat the code by infinite sequence of if-endif definitions.

    debug.hpp

    #ifdef _DEBUG
        #define IF_DEBUG(x) if(x)
    #else
        #define IF_DEBUG(x) if(false)
    #endif
    

    example.cpp

    #include "debug.hpp"
    
    int a,b, ... ,z;
    
    ...
    
    IF_DEBUG(... regular_expression_here_with_a_b_z ...) {
        // set of asserts
        assert(... a ...);
        assert(... b ...);
        ...
        assert(... z ...);
    }
    

    This is not always effective, because compiler may warn you about unused variables has used inside such disabled blocks of code. But at least it is better readable and unused variable warnings can be suppressed for example like this:

    IF_DEBUG(... regular_expression_here_with_a_b_z ...) {
        // set of asserts
        assert(... a ...);
        assert(... b ...);
        ...
        assert(... z ...);
    }
    else {
        (void)a;
        (void)b;
        ....
        (void)z;
    }
    

    It is not always a good idea, but at least helps to reorganize the code.

    0 讨论(0)
  • 2021-01-31 09:20

    If you're doing checks at compile time, you can use Gigi's answer, which will conditionally not compile sections of code. Note that the preprocessor has no knowledge of variables, or sizeof, or other things handled by the compiler (so using something like 4 == sizeof(int) will not fly)

    If you want to compile in little bits of debugging code that should not ever get run, you can use regular conditional statements, like such

    bool debugging = false;
    
    // banana banana banana
    
    if (debugging)
    {
        // do a bunch of stuff here
    }
    

    You can then use a debugger to access the skipped section by assigning debugging to true.

    0 讨论(0)
  • 2021-01-31 09:23

    Synchronously switching on/off chunks of code scattered across the program is sometimes a need too.

    Inspired by this earlier post by Graham I cooked up something like this:

    void doNothing(){}
    #define DO_IF(flag, code) flag ? code : doNothing();
    

    This can be for instance used as follows:

    DO_IF(collectStats, recordStats());
    DO_IF(collectStats, store(pullStat()));
    

    An that is even better:

    #define DO_IF(flag,code) if( flag ) { code; }
    
    0 讨论(0)
  • 2021-01-31 09:23
    1 ? foo() : bar();
    

    This executes foo(). Change the 1 to a 0 to execute bar() instead.

    Unlike the suggestions involving preprocessor directives or comments, both possible branches are compiled, so you get the full benefit of the compiler's syntax checking.

    0 讨论(0)
提交回复
热议问题