home > resources > c++ coding standard > online version

Chapter 6. Best practices

Commenting Out Large Code Blocks

Sometimes large blocks of code need to be commented out for testing. You can't use /**/ style comments because these can't be nested. Surely a large block of your code will contain at least one comment, won't it?

Use #if 0

The easiest way to do this is with an #if 0 block. Don't use #ifdef as someone can unknowingly trigger ifdefs from the compiler command line.

Example 6-1. Commenting Out Large Code Blocks Example

void example()
{
    great looking code
        
    #if 0
    lots of code
    /* a comment */
    some more code
    #endif
        
    more code
}

Use Descriptive Macro Names Instead of 0

The problem with #if 0 is that a while later neither you nor someone else has any idea why this code has been commented out. Is it because a feature has been dropped? Is it because it was buggy? Didn't it compile? Can it be reinstated? It's a mystery.

Therefor you can also chose to use descriptive macro names instead of #if 0.

Example 6-2. Commenting With Descriptive Macro Names

void example()
{
    great looking code
        
    #if NOT_YET_IMPLEMENTED
    travel_through_air();
    #endif
    
    a bit of code

    #if OBSOLETE
    /* a comment */
    travel_by_foot();
    #endif
    
    #if TEMP_DISABLED_OUT_OF_GAS
    travel_by_car();
    #endif
}