Commenting out code

From D Wiki
Jump to: navigation, search

Often, during development, you may wish to comment out certain blocks of code for testing or debugging purposes.

The traditional way

In C, this is often accomplished by inserting a block comment around the code to be disabled:

/*
void brokenFunc() {
    ...
}
*/
void workingFunc() {
    ...
}

The problem with this technique is that it interacts badly with block comments inside the code:

/*
void brokenFunc() {
    int x=123;  /* oops, the outer block comment breaks here */
    int y=321;  /* This part is no longer commented out! */
}
*/
void workingFunc() {
    ...
}

C++ offers a slightly better alternative: insert line comments in front of every line of the code to be commented out:

//void brokenFunc() {
//    int x=123;  /* now this comment no longer causes a problem */
//    int y=321;  /* now this line is also disabled */
//}

void workingFunc() {
    ...
}

You can also use these techniques in D, but D offers better ways to do it.

(In C/C++ you can also use the preprocessor to disable blocks of code, but D doesn't have a preprocessor.)

The D way

The problem with the C++ way is that if brokenFunc() is very long, you have to insert a lot of line comments. Some text editors let you do this with a regex substitution, but still, it's a major hassle for what should be a trivial operation.

D offers several possible solutions.

Nested comments

One method is to use D's nested comments:

/+
void brokenFunc() {
    int x=123;  /* the outer block comment does NOT break here */
    int y=321;  /+ Yes, these comments really do nest!
        /+ (even if the wiki's syntax highlighter fails to handle them properly!) +/
    +/
}
+/
void workingFunc() {
    ...
}

This is superior to the C++ way because you only need to insert two things to disable that block of code: the start of the nested comment, and the end.

However, this still requires that you navigate to the top of the block, then navigate to the bottom of the function, to place those two markers. If you accidentally place a marker in the wrong place, the code will stop compiling. There has to be a better way.

Static if

One better way is to use static if:

// Tada! Just insert a single line and the whole function is now disabled!
// The compiler finds the end of the function for us automatically!
static if (false)
void brokenFunc() {
    int x=123;  /* there is no outer block comment to break here */
    int y=321;  /+ so comments are irrelevant +/
}

void workingFunc() {
    ...
}

Version(none)

Typing "static if (false)" is still somewhat verbose. Better yet is just to use version:

// This saves you a few keystrokes.
// Plus you don't have to parse an if-condition to understand what it does.
version(none)
void brokenFunc() {
    int x=123;  /* there is no the outer block comment to break here */
    int y=321;  /+ so comments are irrelevant +/
}

void workingFunc() {
    ...
}

The good thing about using version is that you can then enable/disable the block of code at will just by changing the identifier inside version:

//version(none)  // this is the original version line
version(all)     // the code is now enabled
void brokenFunc() {
    int x=123;  /* comment freely here */
    int y=321;  /+ to your heart's content +/
}

void workingFunc() {
    ...
}