gSkinner - Home

Comment Blocks That Are Easy to Toggle On/Off

Posted on January 24, 2010 by Grant Skinner

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.

Follow @gskinner on Twitter for more news and views on interactive media.
21 Comments

Thanks Grant! Nice handy tip :)

Posted by: Danny Kopping on Jan 24, 2010 4:24pm URL: http://www.ria-coder.com/blog

Well, I didn't know and it sure is useful so thanks for sharing :)

Posted by: gordee on Jan 24, 2010 4:26pm

I didn't know that, I've setup shortcuts even in the FlashIDE just for that. Handy indeed :)

Posted by: George Profenza on Jan 24, 2010 4:27pm

Great tip :D

Posted by: Tim on Jan 24, 2010 4:32pm

Thank's Grant, very useful :)

Posted by: Hicham on Jan 24, 2010 4:35pm URL: http://hichamtaoufikallah.com

It's so handy~

Posted by: Wooyaggo on Jan 24, 2010 4:35pm URL: 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 on Jan 24, 2010 4:52pm

Yes. this tip can be used a switch of code segment like this

//*/

code segment 1

/*/

code segment 2

//*/

source link

http://ticore.blogspot.com/2008/03/actionscript-multiline-comment-tip.html

Posted by: hbb on Jan 24, 2010 7:40pm

hbb - that's another good one. Thanks for the reminder! I'll update the post with it.

Posted by: Grant Skinner on Jan 24, 2010 8:25pm URL: http://gskinner.com/blog/

Too good.Cool tip and I am ready to implement,much needed at my work.

Posted by: saumya on Jan 24, 2010 9:33pm URL: 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 on Jan 24, 2010 9:56pm URL: http://www.mauriciogiraldo.com/blog

I've used this combination for many years. It's amazing!

Posted by: gheux on Jan 24, 2010 10:47pm URL: http://none

Thank you, very useful info.

Posted by: Pagan on Jan 25, 2010 12:19am

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 on Jan 25, 2010 4:03am URL: http://www.tink.ws/blog

hahaha i love this kind of tricks! very handy indeed!

Posted by: P48l0 on Jan 25, 2010 7:13am URL: 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 on Jan 25, 2010 6:47pm URL: http://www.millermedeiros.com/

thanks for the post, truly a great tip!

Posted by: Tim on Jan 29, 2010 7:43am URL: http://flashscope.com

had to chime in...

nothing 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.

Posted by: thibaud on Feb 4, 2010 5:26pm URL: http://www.thibaud.be

A really nice blog !

Dekla

Posted by: Dekla on Feb 13, 2010 1:11pm URL: http://www.tarifvergleich21.de

Doh, So obvious! Thanks for sharing....

Posted by: Peter on Apr 19, 2010 1:07am URL: http://www.thefuntastic.com

Hi everybody! I want to join the community! What should I Do?

Posted by: Don on Jun 17, 2010 9:43am

Leave a Reply

Your email is never published nor shared.




You may use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">