Where developers come to connect, share, build and be inspired.

14

The //* /*/ //*/ comment toggle trick

96608 views


DISCLAIMER: This is a trick. Do not adopt this trick in production code. Use it wisely while you're doing proof of concept and the like.

So, I guess I'm not the only one that need some way of easily toggling code in and out of the code.

This trick below can be used to toggle between two sets of code, with one symbol in the code with no additional short-keys to learn, and no tool dependencies!

//*                      /*
someCode();              someCode();
/*/                      /*/
someOtherCode();         someOtherCode();
//*/                     //*/

Here is a version for simply commenting code in and out:

Activated:

//*
someCode();
//*/

Deactivated / commented out:

/*
someCode();
//*/

Comments

  • A72c6399a0c8a75f31fc88c470e4a027

    Nice! I've used a similar method in JavaScript like this:

    // On!
    /**/
    var foo = "bar";
    /**/
    
    // Off!
    /** /
    var foo = "bar";
    /**/
    

    The trick is to toggle the space after the second asterisk in the first multiline comment block. Interesting to see other ways to do this as well!

  • 1bcbda8ff96a8250cefd646300521cc1

    A much safer alternative is to use feature flags - http://code.flickr.net/2009/12/02/flipping-out/

  • 06f02967b04f4078dfcb7354c11c4678

    Nice trick, thank you for sharing ! One little reserve: at least in C, this fails if the code to toggle contains comments itself:

    //*
    spawn_unicorns();
    /*/
    slay_the_unicorns(); /* FIXME: This function kills unicorns ! */
    launch_missile();
    //*/
    

    In this code, you won't kill unicorns, but you will always launch a missile, which is not what was intented. It only works in C/C++, but I tend to prefer:

    #if 1
    spawn_unicorns();
    #else
    slay_the_unicorns(); /* FIXME: please don't to that. */
    launch_missile();
    #endif
    

    You just have to change "1" to "0" to toggle between nice and evil code, and it has no restriction regarding embedded comments.

    That said, I'll remember your solution for Java, Javascript and others.

  • 0dfa88c4192d84177ce9f187ac0f7baf

    I just sat in sublime text for like 15 minutes with my mouth agape as I typed forward slash and backspace over and over, watching the syntax highlighting flip back and forth with each keypress. This is beautiful.

  • Feb007acc38e70d57f7b4c205f7e8b26

    @wildlyinaccurate feature flippers do not prevent code from being built, and thus the code is available in the binary. What's safest depends on the definition of save, and the specs of the project. Feature flippers solves a problem (highly relevant to the open/closed principle) but not the problem this trick solves.

  • 35862cd

    I wouldn't normally comment, but this has a lot of people looking at it. Don't do this. For reasons why, see the hacker news thread https://news.ycombinator.com/item?id=5625490

  • Aa50860353d6c6bba575cefe31c5709f

    I agree that commented-out code should not be part of production code.

    But I use this trick all the time while writing/debugging/updating/fixing code.

    It makes enabling/disabling code blocks extremely quick and easy.

  • 9a378ed2410bdabed82883f2615f6b61

    I came to mention the #if 0 version only to find that it was already mentioned. Too bad it doesn't work in other languages as well :(

  • Feb007acc38e70d57f7b4c205f7e8b26

    @mikbe you should never work on production code with this trick. Then someone is doing something wrong. This is just a trick that's fun to use while testing out two different approaches. Such as an iterative versus a recursive algorithm.

  • Feb007acc38e70d57f7b4c205f7e8b26
  • 47fb157b1aece9d4e86d8d0211968cc9

    What editor are you using?

  • Feb007acc38e70d57f7b4c205f7e8b26

    @cguillemette I'm using 8 editors, Visual Studio, Sublime Text 2/3, Mono Develop, Notepad++, Eclipse, NetBeans, MS SQL Management Studio and Vim.

  • 9ebec172e390886892c81c90b3d23058

    oh god. this is what my old company used to control database connections. jdbc in development (tomcat) and jndi lookup everywhere else (websphere i think?). yes, we had to modify code to move to another environment! I couldn't explain this enough to my manager that this was a bad thing.

    that was the least of the issues. it was on the whole, written by people who didn't know how the hell to use java. (probably former SQL devs?)

  • 31af496eba4f132acb8cd51b2cf59a0d

    I just have a shortcut in emacs that comments and uncomments things in all languages seamlessly.

  • C9a56d91119a96b8f5ef65e28161f4eb

    Sublime text 2 also does that by default :)

  • User-avatar

    @ralphholzmann Super-slick trick!

  • D536d55d0165621695c7bcdd5fceb73d

    I would hate having to work on code with this trick. It would be hugely confusing.

    I accept that your probably a lot smarter than me so have no problem with it but I always strive to write code like other people are going to read it. Part of that is not writing cleverly so much as writing clearly.

  • User-avatar

    Nice to see it published...but this trick has been around for a quite a few years now.

  • User-avatar

    I see no issues with this as long as you don't check it in or deploy to production like this. For local/debug development, this can be quite useful. If you need a reminder, you could add a commented line with a reminder (like "TODO: remove toggle") and do a Find before checking in.

  • E7426a813d3bab6b1758f06c44fe0f4b

    I also have been doing this PHP. @ralphholzmann's version is a better one in my opinion, because it can be used multiple times after each other. Allowing you to toggle multiple stacks.

  • A578c0020af1cda07eba4f3672aa32c1

    @nekwebdev Sublime Text 2 is the best, but Brackets is definitely close behind! I love Brackets so far but JSLint pisses me off, haha. If you haven't tried Brackets out yet, I strongly suggest that you do: http://brackets.io/

  • 70311bce5ef94cd07b5b0da8c2f8461e

    This one is awesome!

  • 09c8097b695bc26df2979cf39fe4ccc0

    is it really hard to cmd-slash ?

  • ___-1____normal

    I see no issues with this as long as you don't check it in or deploy to production like this. http://www.obd2works.com/

  • E633424f80ce24effe8a115ec92a00ff

    For those writing JavaScript with r.js compiler, pragmas can be very useful for this kind of thing.

  • Default_profile_6_normal

    It makes enabling/disabling code blocks extremely quick and easy. more

  • David

    thanks, i used something similar too.

  • Default_profile_5_normal

    my roomate's aunt makes $88 an hour on the laptop . She has been out of a job for 10 months but last month her payment was $21559 just working on the laptop for a few hours. learn this here now .... http://www.wikiwages.com

  • 0__7piwaqd0yhjo9un7oeckvnhrjhx2q6cf7xiuvuhxyz5296q5w6ctmvdzsgzexz9qwxc3vzwprgl7cbnopj0tvq59rgk7gcv7pjher3ejvch5qmqis7mo7fmnm4n6gzi6xgfdwy3dz4

    I just lay throughout stylish text regarding similar to a quarter-hour with my own mouth area agape as i tapped out forwards reduce along with backspace often, enjoying the format displaying use backwards and forwards with every single keypress. It is gorgeous. http://obdscannerhq.com/

  • None
  • None
  • X3vh8hrx_normal
  • X3vh8hrx_normal

Add a comment