Sometimes when you're coding, you find yourself toggling a block of code on or off by commenting it. Most development environments make this pretty easy with a button or shortcut that will comment or uncomment a selected block.
However, it can be handy to know that there's a way to achieve the same thing by setting up your block comments in such a way that they can be enabled or disabled by adding or removing a single "/".
Here's how it looks with the code commented out:
/*
thisIsCodeIWant.toToggle(onOff);
andMoreCodeHere();
//*/
You can uncomment the code block by adding a "/" at the start of the block comment like so:
//* thisIsCodeIWant.toToggle(onOff); andMoreCodeHere(); //*/
After I posted this, hbb reminded me of a related commenting trick. With the following set-up you can toggle between two code blocks by adding a "/".
//* thisIsCodeBlock(1); moreCode(); /*/ thisIsCodeBlock(2); andMore(); //*/
Just remove the "/" at the beginning of the first block comment to swap code blocks.
/* thisIsCodeBlock(1); moreCode(); /*/ thisIsCodeBlock(2); andMore(); //*/
This is a pretty basic tip, but I've found that not all developers are aware of it, so I thought it couldn't hurt to share it.


Comments (21)
Thanks Grant! Nice handy tip :)
Posted by: Danny Kopping at January 24, 2010 04:24 PMURL: http://www.ria-coder.com/blog
Well, I didn't know and it sure is useful so thanks for sharing :)
Posted by: gordee at January 24, 2010 04:26 PMURL:
I didn't know that, I've setup shortcuts even in the FlashIDE just for that. Handy indeed :)
Posted by: George Profenza at January 24, 2010 04:27 PMURL:
Great tip :D
Posted by: Tim at January 24, 2010 04:32 PMURL:
Thank's Grant, very useful :)
Posted by: Hicham at January 24, 2010 04:35 PMURL: http://hichamtaoufikallah.com
It's so handy~
Posted by: Wooyaggo at January 24, 2010 04:35 PMURL: http://as3.kr
I use the CMD+/ or CTRL+/ in eclipse (using Flash Builder plugin) to toggle // comments on each highlighted line. There's also CMD+SHIFT+C or CTRL+SHIFT+C for MXML files to block comment MXML.
Posted by: Brandon at January 24, 2010 04:52 PMURL:
Yes. this tip can be used a switch of code segment like this
//*/
code segment 1
/*/
code segment 2
//*/
source link
Posted by: hbb at January 24, 2010 07:40 PMhttp://ticore.blogspot.com/2008/03/actionscript-multiline-comment-tip.html
URL:
hbb - that's another good one. Thanks for the reminder! I'll update the post with it.
Posted by: Grant Skinner at January 24, 2010 08:25 PMURL: http://gskinner.com/blog/
Too good.Cool tip and I am ready to implement,much needed at my work.
Posted by: saumya at January 24, 2010 09:33 PMURL: http://www.saumyaray.wordpress.com
what i use is:
/**
something
/**/
when i need to uncomment i just add a /:
/**/
something
/**/
similar solution
Posted by: mga at January 24, 2010 09:56 PMURL: http://www.mauriciogiraldo.com/blog
I've used this combination for many years. It's amazing!
Posted by: gheux at January 24, 2010 10:47 PMURL: http://none
Thank you, very useful info.
Posted by: Pagan at January 25, 2010 12:19 AMURL:
I go with the shortcuts Brandon posted.
This means that when code is uncommented, I don't leave lines such as '//*' and '//*/' that where used for comments to be tidied up later, or hanging around for ever.
Posted by: Tink at January 25, 2010 04:03 AMURL: http://www.tink.ws/blog
hahaha i love this kind of tricks! very handy indeed!
Posted by: P48l0 at January 25, 2010 07:13 AMURL: http://tracehello.wordpress.com
I use a slightly different technique.
/* */
something
/* */
another thing
/* */
when I need to comment just need to add 1 space:
/* * /
something commented
/* */
another thing
/* */
and it can be used to many blocks of code.. just requires adding/removing one space at each code block.
Posted by: Miller Medeiros at January 25, 2010 06:47 PMURL: http://www.millermedeiros.com/
thanks for the post, truly a great tip!
Posted by: Tim at January 29, 2010 07:43 AMURL: http://flashscope.com
had to chime in...
Posted by: thibaud at February 4, 2010 05:26 PMnothing beats flashdevelop when it comes to code commenting.
ctrl+c (toggle line comments).
ctrl+shift+c (toggle block comments).
being able to toggle on/off by just having the cursor in the commented block or on the line you wish to comment is just awesome.
URL: http://www.thibaud.be
A really nice blog !
Dekla
Posted by: Dekla at February 13, 2010 01:11 PMURL: http://www.tarifvergleich21.de
Doh, So obvious! Thanks for sharing....
Posted by: Peter at April 19, 2010 01:07 AMURL: http://www.thefuntastic.com
Hi everybody! I want to join the community! What should I Do?
Posted by: Don at June 17, 2010 09:43 AMURL: