The One Character Block Comment

When debugging, I often find that I have to comment and un-comment a block of code several times during the process of trying to find out what’s going on. That used to mean typing and deleting comment block characters repetitively, but not anymore. Here’s a simple solution to that problem: Comment or un-comment an entire code block of code by typing or deleting a single character.

I was able to arrive at this solution by combining the one-line comment with the comment block in a way that takes advantage of the rules the different types of comments have to follow.

The One-Line Comment

One-line comment rules dictate that everything after the comment characters must be ignored for the rest of that line. They look like this:

1
2
3
4
5
// My code below
$someVar = array("foo", "bar", "blah");
echo "<code>";
print_r($someVar);
echo "</code>";

The Block Comment

Block comment rules dictate that once the beginning characters are started, everything up until the ending characters is ignored. They look like this:

1
2
3
4
5
6
/* My code below
$someVar = array("foo", "bar", "blah");
echo "<code>";
print_r($someVar);
echo "</code>";
*/

Combining The Two: The Best of Both Worlds

Using both those comment style rules, we can combine the two comment styles in a way that will allow us to comment and un-comment a block of code with by adding or deleting a single character. The trick is to make both the beginning and ending lines both single-line AND block style comments, like so:

1
2
3
4
5
6
//* My code below
$someVar = array("foo", "bar", "blah");
echo "<code>";
print_r($someVar);
echo "</code>";
// */

In the above example, you notice that the code will still run – it’s not commented out because the first line is following the rules of the single-line comment – ignoring the block comment declaration on the same line. By removing the first backslash at the beginning of the block, we can comment out the entire code block by enabling the block comment style instead of the single-line style. The last single-comment line is ignored because the block comment rules ignore everything up until the end block comment declaration at the end of the line:

1
2
3
4
5
6
/* My code below
$someVar = array("foo", "bar", "blah");
echo "<code>";
print_r($someVar);
echo "</code>";
// */

So a single character – a backslash at the very beginning of the block declaration can now comment or un-comment the entire block by switching the comment rules back and forth between single-line and block style. The code examples provided are in PHP, but this trick will work with any language that supports both single-line and block style comments.

Hire Me

Have a problem you need help solving?
I'm available for freelance work. (and I love solving hard problems)

Trackbacks

Comments

  1. If you want to toggle between two blocks of code, you can further do this:

    //*
    Version 1…
    /*/
    Version 2…
    // */

    Current Version 1 is run, but it will run version 2 if you remove the first slash at the top.

  2. @Mark.

    I find preprocessor commands far more elegant when it comes to swapping out two different versions of code, when only one needs to be compiled.

  3. A very ingenious solution, but I prefer to let my IDE do the work. I just select the code and hit the hotkey to comment/uncomment.

  4. Very clever.

    But unfortunately, there’s people who insist on using /* */ comments everywhere, and it makes it impossible to use this sort of technique on their code.

    Which is why, when I import code into a project, the first thing I do is change all the comments over to “//” style comments.

  5. Dude, it’s called comment/uncomment commands. In Emacs I have the f8 key mapped to commenting a selected block of code and f7 to comment a line.

    Make the computer do as much of the work as you can!

  6. Use a real editor.

    kb

  7. Generally, when I want to be able to toggle a block of code, I put it in something like this:

    if(0) {
    // toggleable code here
    }

    It’s also only a single character change to toggle it, but it has the benefit of being able to switch between two different implementations with the addition of an else clause. Of course, it requires that the code not have syntax errors.

  8. Agree with letting the IDE do the work. Visual Assist shortcuts are Ctrl-K, C for comment and Ctrl-K, U for uncomment.

    Though I’ll admit I did spend quite a bit of time looking for tricks like this back when I used to code using notepad and gcc.

  9. Another similar way is to use this (for commented in):

    /**/
    Code
    /**/

    And then ro comment it out just take out the second forward slash:

    /**
    Code
    /**/

    I personally find it easier to ready and type.

  10. I think some of us are missing the point:
    + the trick of this article is IDE independent
    + it’s pretty simple and requires a minimum of extra coding during the programming process (the // * / )
    + using a mouse, click on the beginning of the line and DEL key
    – keyboard: get to the line, HOME, DEL
    –> no selecting!

    Nice post

  11. If you still using notepad for programming then its good technique :) but I prefer you, use IDE .

  12. Great post Vance!

    Never mind the haters with their negative comments. Ideas like this show creative thinking while coding, this is a good thing.

    HACK TO LEARN
    peace

  13. Vance,

    Interesting methodology… I would like to offer an alternative solution to the issue you presented.

    Rather than depending on echo statements to debug (which have to be turned on and off), I started using phpunit (http://phpunit.de). Now I can ensure my application performs as expected without continually editing the file.

    “Whenever you are tempted to type something into a print statement or a debugger expression, write it as a test instead.” – Martin Fowler

    -Ken Nordquist

  14. This is such a simple solution and so very effective. I’m surprised I hadn’t heard of it before. You’ve definitely spared me from some pretty severe headaches since I read this.

  15. Great post Vance!

  16. :1,10s#^#//#

    :1,10s#^//##

    i find it more reassuring to know that there’s nothing left to chance and no way that other comments could interfere with the code.

  17. ,..] http://www.vancelucas.com is other nice source of tips on this subject,..]

  18. thank you for the tips

  19. @polak I dont think you understood this correctly…

  20. thanks! bookmarked :D

  21. Fiegreeflinna
    rbgd

  22. cool!!)))

All content copyright © 2013 Vance Lucas | Powered by WordPress | Entries (RSS) | Comments (RSS)