Block-level comments trick

Block-level comments are useful for commenting out an entire block of code in PHP, CSS, and other code contexts.

/*
$this = 'code is deactivated';
$and = 'so is this';
*/

The only problem with this is that when you go to re-activate this code, you have to change both the opening and closing comment markers. That’s a pain.

While I was at WordCamp NYC last week, I saw Daisy Olsen using a very clever trick in her lightning round talk.

/*
$this = 'code is deactivated';
$and = 'so is this';
/**/

See what she did there? The closing comment marker is preceded by another opening marker. Because comment blocks can’t be nested, this second opening comment marker is ignored. This enabled her to re-enable this code by removing the opening marker.

$this = 'code is reactivated';
$and = 'so is this';
/**/

Brilliant! I can’t believe I haven’t seen this before. The one downside to this is that you are deleting two characters and destroying the opening marker. Here’s an even better method.

//*
$this = 'code is reactivated';
$and = 'so is this';
/**/

By adding a slash in front of the opening comment marker, I comment out the comment marker. It only takes one key press, and the corpse of the original opening marker is retained, allowing you to reinstate it with the deletion of a single character.

Props to Aleem Bawany for the second trick (he uses //*/ as the closing comment, which works pretty much the same way).

What other commenting tricks do you know?

37 thoughts on “Block-level comments trick

  1. Brilliant is the perfect word for this…

    I saw her do this too but thought it was something that everyone was already doing that I should have already known about… So, I kept quiet haha!

    I imagine the same applies to comments in HTML and JS too, but haven’t needed to try it yet.

  2. I’m no code monkey (quite the opposite, in fact), but I’ve known about this for years. Can’t believe you missed out on such a cool trick.

  3. Awesome. I tend to delete/add whole lines at a time, so I’ll be rocking the first trick from now on.

    Though the second trick is handy because you can see where the comment block used to be. Hmm….

    To whom goes the props for the first trick? Who is the mysterious “she”?

  4. I, too, saw her do that and in the hectic aftermath of WCNYC, I completely forgot it.

    /Very/ cool.

    (See what I did there? *grin*)

  5. I must be weird.

    I delete the code and rely on source control to bring it back for me :-)

    Too many of my co-workers leave the code there commented out even after they have written something new.

    • Westi, not weird, but during development I find this technique is useful just to make sure that the code I just wrote is what broke everything :-)

      I generally don’t leave it in when I’m ready to commit to subversion.

      Mike

  6. Heh, nice one. I’m doing a fork of a C program, and I’m commenting out everything unnecessary, but I’m not deleting the code yet. So, if I make a mistake, I can uncomment that block of code.

    Really useful, thanks.

  7. Jeremy Clarke says:

    Definitely a neat trick, though I have to agree with Eric Marden. If your code editor is worth anything it should have a bulk comment/uncomment command mapped onto cmd / that makes this trick unnecessary.

    It blew my mind when I saw it of course. It seems so obvious once you see it, and there are definitely some rare situations where it would make things a lot easier.

  8. So, if you use this a lot your code is going to be littered with /**/’s? That doesn’t excite me.

    Also, is it a good idea to redefine $this? (jk)

  9. Weird, I was doing some style testing on a new theme just last week and started using this same method to switch back and forth in my stylesheet:

    /*NORMAL*/ background: #f3f3f3; /**/
    /*NEW*//* background: #c191b8; /**/

    I was tired of changing back and forth the “old way” in multiple places and did this out of convenience. Just adding and removing one /*.

    I wouldn’t leave this in for production code but it is great for quick testing. Maybe that is why we haven’t seen it more?

  10. So I *might* have seen that trick somewhere but I’m pretty sure I found it by accident. It’s pretty helpful for doing demos. I’ll start using Mark’s tweaked version since one keystroke is better than two :-)

  11. Chris says:

    In languages that don’t support // style comments, you can do:

    /**
    body { … }
    a { … }
    /**/

    And then enable the code with:

    /**/
    body { … }
    a { … }
    /**/

    I like this method ’cause it’s more universal.

    • Yeah, that’s how I’ve always done it.

      Of course, if the block of code you’re testing already has comments within it, you have to do something else, like wrap it in an if (false) { ... } block.

  12. Thomas says:

    To toggle between two block of code do this to activate the first block:
    //*/
    a = 1;
    /*/
    a = 2;
    //*/
    and simply remove one slash to switch to the second block.
    /*/
    a = 1;
    /*/
    a = 2;
    //*/

    • benjam says:

      Ok, I discovered the //*/ … //*/ trick on my own, and have been using it for years, but /that/ is cool.

      Thanks for sharing that, Thomas

  13. ash and misty pokemon having sex In my opinion you stole this article and placed on another site. I had already seen. The author, and you are out of town? Most people are happy as much as they have decided to be happy. – A. Lincoln cool The morning began with a wonderful article, thank you! Who is mulberry? All true, but how else? Wine and women bring to us Interestingly, and cognitive, and will have something on this subject?

  14. sex reassignment You normally happy about us with the best phrases thanks, take! Well without untoward happens – even a schoolboy on holiday give job. – Charles Lamb Blog is excellent, plenty to like! Blog is excellent, plenty to like! The site just super, I will recommend to friends! super original Quite strange material you provide to us Really? amazing.

Comments are closed.