zbc2zw
Last Updated: October 12, 2018
·
110.7K
· alexanderbrevig

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

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();
//*/

38 Responses
Add your response

6026

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!

over 1 year ago ·
6027

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

over 1 year ago ·
6028

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

over 1 year ago ·
6029

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.

over 1 year ago ·
6030

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

over 1 year ago ·
6031
6032

What editor are you using?

over 1 year ago ·
6033

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

over 1 year ago ·
6037

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 :(

over 1 year ago ·
6040

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?)

over 1 year ago ·
6044

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

over 1 year ago ·
6045

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.

over 1 year ago ·
6235

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.

over 1 year ago ·
6349

Sublime text 2 also does that by default :)

over 1 year ago ·
6477

@ralphholzmann Super-slick trick!

over 1 year ago ·
6710

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.

over 1 year ago ·
6712

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

over 1 year ago ·
6713

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

over 1 year ago ·
6719

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.

over 1 year ago ·
6720

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.

over 1 year ago ·
7544

@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/

over 1 year ago ·
9314

This one is awesome!

over 1 year ago ·
9926

is it really hard to cmd-slash ?

over 1 year ago ·
10879

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

over 1 year ago ·
16810

thanks, i used something similar too.

over 1 year ago ·
27915

Here's a pair of tricks that should prove useful in languages with C++-like comment syntax.
https://www.gangboard.com/app-programming-scripting-training/angularjs-training

over 1 year ago ·
28418

http://bloonstowerdefense5game.com
This is such a great resource that you are providing and you give it away for free. I love
seeing that understand the value of providing a quality resource for free.
http://supersmashflash-2.com

over 1 year ago ·
28722

Thanks .
In eclipse ,
select text to comment and press Shift+Ctrl+/
select text to uncomment and press Shift+Ctrl+\

over 1 year ago ·
29053

what is the advantage of this over single and multi-line commenting by clicking the line number, or selecting multiple lines then pressing "command(or ctrl) + /"?

over 1 year ago ·
29779

That is such a great learning resource that you will be providing and you simply give it away free of charge. I love since understand the worthiness of providing an excellent resource free of charge.
https://www.rprservices.com/property-preservation-data-processing/

7 months ago ·
29780

I am always searching on the internet for articles that will help me. There is actually too much to find out about this. I believe you made the right details in Features also. Keep working, congrats !
https://charmyposh.com/collections/carlos-by-carlos-santana

7 months ago ·
29785

I beloved up to you'll obtain performed right here. The cartoon is attractive, your authored subject matter stylish. nonetheless, you command get got an shakiness over that you would like be handing over the following. in poor health without a doubt come more earlier once more as exactly the similar nearly a lot ceaselessly within case you defend this hike. https://instadp.org

7 months ago ·
29819

Your site has a lot of useful information for myself. I visit regularly. Hope to have more quality items.
<a href="http://colorswitch2.com/" >color switch</a>

6 months ago ·
29935

I think this is an informative post and it is very useful and knowledgeable. I really enjoyed reading this post. big fan, thank you!
http://impossiblegame.co/

5 months ago ·
30141

I've read all your post and It's really amazing , I can say. Keep it up and I will follow every single one of them :)
https://hide-online.com/

3 months ago ·
30418

I am always searching on the internet for articles that will assist me. There is really too much to learn about this. I really believe you made the right details in Features also. Keep working, Thanks.
http://www.artisanalminers.io/

about 2 months ago ·
30679

Yes, this is my first time to your blog! Your blog gives us valuable information. You did a marvelous job
http://earntodie.co/

about 1 month ago ·
30872

Your article is very meaningful, the content is also quite good and impressive, I hope the next time you will have more good and meaningful articles to give readers the knowledge.
https://www.gettyimages.co.uk/detail/news-photo/richard-caring-and-patricia-caring-attend-the-argento-ball-news-photo/985560576

13 days ago ·