Comment Blocks That Are Easy to Toggle On/Off

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.

Grant Skinner

The "g" in gskinner. Also the "skinner".

@gskinner

22 Comments

  1. Thanks Grant! Nice handy tip πŸ™‚

  2. Well, I didn’t know and it sure is useful so thanks for sharing πŸ™‚

  3. George Profenza January 24, 2010 at 4:27pm

    I didn’t know that, I’ve setup shortcuts even in the FlashIDE just for that. Handy indeed πŸ™‚

  4. Great tip πŸ˜€

  5. Thank’s Grant, very useful πŸ™‚

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

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

  8. hbb – that’s another good one. Thanks for the reminder! I’ll update the post with it.

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

  10. what i use is:

    /**

    something

    /**/

    when i need to uncomment i just add a /:

    /**/

    something

    /**/

    similar solution

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

  12. Thank you, very useful info.

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

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

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

  16. thanks for the post, truly a great tip!

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

  18. A really nice blog !

    Dekla

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

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

  21. More generally toggle odd / even blocks extending this technique :

    //*/

    $a = 1;

    /*/

    $b = 1;

    /*/

    $c = 1;

    /*/

    $d = 1;

    /*/

    $e = 1;

    /*/

    $f = 1;

    /*/

    $g = 1;

    /*/

    $h = 1;

    /*/

Comments are closed.