# Neat commenting trick i just discovered.

## Recommended Posts

If you have commented out segments of code you want to toggle on and off you can do this. Start of with the regular "/*" but end your the section with "//*/" Now if you want to toggle change "/* to "//*"
//*
Some code
more code
even more code
//*/

/*
Some code
more code
even more code
//*/


Looks like it doesnt work in the forums source tags.

##### Share on other sites
Neat

i do:

#define DEUBG_1 1
#define DEBUG_2 0

#if DEBUG_1

...
...

#elif DEBUG_2

...
...

#endif

most development editors have mass commenting in and out for selections, VSC++, emacs.

##### Share on other sites
Quote:
 Original post by iminyourbrainmost development editors have mass commenting in and out for selections, VSC++, emacs.

Yeah I know that. Though this does have a certain convenience to it. With those you have to highlight the entire section of text first. With this each segment of comment is controlled with one key stoke an the that control is placed right at the start of the code segment. And you don't have to hunt for the close comment. Also When the code is toggled on it still marks that code as something you may want to turn off later if you forgotten about it.

##### Share on other sites
If you want to toggle between two pieces of code you can even use:
/*code A/*/code B  //active//*///*code A  //active/*/code B//*/

[COOL]

##### Share on other sites
Quote:
 Original post by samvIf you want to toggle between two pieces of code you can even use:/*code A/*/code B //active//*///*code A //active/*/code B//*/[COOL]
Oh, very nice. I'm sure ill make use of this.

##### Share on other sites
That just seems like a mistake waiting to happen.

CM

##### Share on other sites
Instead of using comment, I usually do sth like this:

#if 0 // toggle to switch
// disabled code
#else
// enabled code
#endif

##### Share on other sites
The last line should be:
// */
to avoid compiler warnings, iirc.
(Note the space)

this is sick

I concur.

##### Share on other sites
Actually the main advantage (compared to #if etc.) is that the syntax highlighting immediately tells me what part is "active", so it can reduce the risk of confusion.

Ofcourse, there are disadvantages (increased risk of confusing other people, a lot less flexible, interference with real block comments, etc.), so it's a trade-off.

##### Share on other sites
In VS2k5, the syntax highlighting greys out sections like #if 0 too.

##### Share on other sites
Quote:
 Original post by Conner McCloudThat just seems like a mistake waiting to happen.

I agree.

As with most bad things, this seems like a symptom of other problems. In almost all cases, it's better to not have conditionally compiled code hanging around. That's what revision control is for.

##### Share on other sites
Quote:
 Original post by SpoonbenderIn VS2k5, the syntax highlighting greys out sections like #if 0 too.

I just installed it and tried this. Very nice! It takes a considerably delay though.

Quote:
 Original post by JasonBlochowiakAs with most bad things, this seems like a symptom of other problems. In almost all cases, it's better to not have conditionally compiled code hanging around. That's what revision control is for.

In the long term certainly, yes. But I'd rather use this "hack" to quickly change things back and forth a few times to compare them, if it helps to keep the change-compile-run iteration time down.
The "other problem" in this case seems to be (a mix of) inadequate editor UI, archaic compilation model and/or the lack of a local micro-revision control mechanism.

##### Share on other sites
Also, in VC++ 2005, you can select a block of text and do ctrl-k-c to comment all selected lines with a //, and ctrl-k-u to uncomment.

##### Share on other sites
Quote:
 Original post by JaymarAlso, in VC++ 2005, you can select a block of text and do ctrl-k-c to comment all selected lines with a //, and ctrl-k-u to uncomment.
Yup...

##### Share on other sites
Quote:
Original post by samv
Quote:
 Original post by SpoonbenderIn VS2k5, the syntax highlighting greys out sections like #if 0 too.

I just installed it and tried this. Very nice! It takes a considerably delay though.

Vim does it too, and fast - just like all the other syntax highlighting. Of course, it doesn't compile-as-you-go (at least not with any mods that I know of ;) and certainly not out-of-box)

##### Share on other sites
A little commmenting thing that I just noticed:

	//This should crash:\\	blah();

blah(); gets commented out. I had never heard of this before, but if you end a comment with forward slashes (or are they backslashes, I never can remember!), the next line will be commented out, too.

I can't think of a good use for this, though.

[Edit: this apparently doesn't work inside of source brackets, but it shure does in VC++ 2005.

##### Share on other sites
char str[] = "abcdefg\              hijk";

edit: bah. didn't like the backslash =/

##### Share on other sites
/ - forward slash
\ - backslash

Terminating a line with a backslash in C and C++ source code (and a few other C-based languages) causes a line continuation, such that the next line is considered syntactically part of the same expression. Necator pointed this out:
char str[] = "abcdefg\\               hijk";

The problem with his code snippet is that the string now includes all the spaces used to indent the fragments, so the resulting line of code is:
char str[] = "abcdefg             hijk";

Another interesting C source behavior is to concatenate syntactically adjacent strings:
char str[] = "abcdefg"             "hijk";   // resulting string: "abcdefghijk"

##### Share on other sites
Ah, I see. That makes sense now. And thanks for the clarification about forward and backslashes, though I'll have forgotten it in about 3 minutes.