https://wiki.dlang.org/api.php?action=feedcontributions&user=LinAGKar&feedformat=atomD Wiki - User contributions [en]2024-03-29T06:39:19ZUser contributionsMediaWiki 1.31.2https://wiki.dlang.org/?title=Commenting_out_code&diff=8444Commenting out code2017-05-31T09:44:40Z<p>LinAGKar: /* The traditional way */</p>
<hr />
<div>Often, during development, you may wish to comment out certain blocks of code for testing or debugging purposes.<br />
<br />
==The traditional way==<br />
<br />
In C (before C99), this is accomplished by inserting a block comment around the code to be disabled:<br />
<br />
<syntaxhighlight lang=C><br />
/*<br />
void brokenFunc() {<br />
...<br />
}<br />
*/<br />
void workingFunc() {<br />
...<br />
}<br />
</syntaxhighlight><br />
<br />
The problem with this technique is that it interacts badly with block comments inside the code:<br />
<br />
<syntaxhighlight lang=C><br />
/*<br />
void brokenFunc() {<br />
int x=123; /* oops, the outer block comment breaks here */<br />
int y=321; /* This part is no longer commented out! */<br />
}<br />
*/<br />
void workingFunc() {<br />
...<br />
}<br />
</syntaxhighlight><br />
<br />
C++ and C99 offers a slightly better alternative: insert line comments in front of every line of the code to be commented out:<br />
<br />
<syntaxhighlight lang="C"><br />
//void brokenFunc() {<br />
// int x=123; /* now this comment no longer causes a problem */<br />
// int y=321; /* now this line is also disabled */<br />
//}<br />
<br />
void workingFunc() {<br />
...<br />
}<br />
</syntaxhighlight><br />
<br />
You can also use these techniques in D, but D offers better ways to do it.<br />
<br />
(In C/C++ you can also use the preprocessor to disable blocks of code, but D doesn't have a preprocessor.)<br />
<br />
==The D way==<br />
<br />
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.<br />
<br />
D offers several possible solutions.<br />
<br />
===Nested comments===<br />
<br />
One method is to use D's nested comments:<br />
<syntaxhighlight lang=D><br />
/+<br />
void brokenFunc() {<br />
int x=123; /* the outer block comment does NOT break here */<br />
int y=321; /+ Yes, these comments really do nest!<br />
/+ (even if the wiki's syntax highlighter fails to handle them properly!) +/<br />
+/<br />
}<br />
+/<br />
void workingFunc() {<br />
...<br />
}<br />
</syntaxhighlight><br />
<br />
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.<br />
<br />
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.<br />
<br />
===Static if===<br />
<br />
One better way is to use '''static if''':<br />
<br />
<syntaxhighlight lang=D><br />
// Tada! Just insert a single line and the whole function is now disabled!<br />
// The compiler finds the end of the function for us automatically!<br />
static if (false)<br />
void brokenFunc() {<br />
int x=123; /* there is no outer block comment to break here */<br />
int y=321; /+ so comments are irrelevant +/<br />
}<br />
<br />
void workingFunc() {<br />
...<br />
}<br />
</syntaxhighlight><br />
<br />
===Version(none)===<br />
<br />
Typing "static if (false)" is still somewhat verbose. Better yet is just to use '''version''':<br />
<br />
<syntaxhighlight lang=D><br />
// This saves you a few keystrokes.<br />
// Plus you don't have to parse an if-condition to understand what it does.<br />
version(none)<br />
void brokenFunc() {<br />
int x=123; /* there is no the outer block comment to break here */<br />
int y=321; /+ so comments are irrelevant +/<br />
}<br />
<br />
void workingFunc() {<br />
...<br />
}<br />
</syntaxhighlight><br />
<br />
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''':<br />
<br />
<syntaxhighlight lang=D><br />
//version(none) // this is the original version line<br />
version(all) // the code is now enabled<br />
void brokenFunc() {<br />
int x=123; /* comment freely here */<br />
int y=321; /+ to your heart's content +/<br />
}<br />
<br />
void workingFunc() {<br />
...<br />
}<br />
</syntaxhighlight><br />
<br />
[[Category:CommonIdiom]]</div>LinAGKarhttps://wiki.dlang.org/?title=Commenting_out_code&diff=8443Commenting out code2017-05-31T09:43:51Z<p>LinAGKar: /* The traditional way */</p>
<hr />
<div>Often, during development, you may wish to comment out certain blocks of code for testing or debugging purposes.<br />
<br />
==The traditional way==<br />
<br />
In C (before C99), this is often accomplished by inserting a block comment around the code to be disabled:<br />
<br />
<syntaxhighlight lang=C><br />
/*<br />
void brokenFunc() {<br />
...<br />
}<br />
*/<br />
void workingFunc() {<br />
...<br />
}<br />
</syntaxhighlight><br />
<br />
The problem with this technique is that it interacts badly with block comments inside the code:<br />
<br />
<syntaxhighlight lang=C><br />
/*<br />
void brokenFunc() {<br />
int x=123; /* oops, the outer block comment breaks here */<br />
int y=321; /* This part is no longer commented out! */<br />
}<br />
*/<br />
void workingFunc() {<br />
...<br />
}<br />
</syntaxhighlight><br />
<br />
C++ offers a slightly better alternative: insert line comments in front of every line of the code to be commented out:<br />
<br />
<syntaxhighlight lang="C"><br />
//void brokenFunc() {<br />
// int x=123; /* now this comment no longer causes a problem */<br />
// int y=321; /* now this line is also disabled */<br />
//}<br />
<br />
void workingFunc() {<br />
...<br />
}<br />
</syntaxhighlight><br />
<br />
You can also use these techniques in D, but D offers better ways to do it.<br />
<br />
(In C/C++ you can also use the preprocessor to disable blocks of code, but D doesn't have a preprocessor.)<br />
<br />
==The D way==<br />
<br />
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.<br />
<br />
D offers several possible solutions.<br />
<br />
===Nested comments===<br />
<br />
One method is to use D's nested comments:<br />
<syntaxhighlight lang=D><br />
/+<br />
void brokenFunc() {<br />
int x=123; /* the outer block comment does NOT break here */<br />
int y=321; /+ Yes, these comments really do nest!<br />
/+ (even if the wiki's syntax highlighter fails to handle them properly!) +/<br />
+/<br />
}<br />
+/<br />
void workingFunc() {<br />
...<br />
}<br />
</syntaxhighlight><br />
<br />
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.<br />
<br />
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.<br />
<br />
===Static if===<br />
<br />
One better way is to use '''static if''':<br />
<br />
<syntaxhighlight lang=D><br />
// Tada! Just insert a single line and the whole function is now disabled!<br />
// The compiler finds the end of the function for us automatically!<br />
static if (false)<br />
void brokenFunc() {<br />
int x=123; /* there is no outer block comment to break here */<br />
int y=321; /+ so comments are irrelevant +/<br />
}<br />
<br />
void workingFunc() {<br />
...<br />
}<br />
</syntaxhighlight><br />
<br />
===Version(none)===<br />
<br />
Typing "static if (false)" is still somewhat verbose. Better yet is just to use '''version''':<br />
<br />
<syntaxhighlight lang=D><br />
// This saves you a few keystrokes.<br />
// Plus you don't have to parse an if-condition to understand what it does.<br />
version(none)<br />
void brokenFunc() {<br />
int x=123; /* there is no the outer block comment to break here */<br />
int y=321; /+ so comments are irrelevant +/<br />
}<br />
<br />
void workingFunc() {<br />
...<br />
}<br />
</syntaxhighlight><br />
<br />
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''':<br />
<br />
<syntaxhighlight lang=D><br />
//version(none) // this is the original version line<br />
version(all) // the code is now enabled<br />
void brokenFunc() {<br />
int x=123; /* comment freely here */<br />
int y=321; /+ to your heart's content +/<br />
}<br />
<br />
void workingFunc() {<br />
...<br />
}<br />
</syntaxhighlight><br />
<br />
[[Category:CommonIdiom]]</div>LinAGKar