Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Toggling Between Alternative Implementations Using Comments

DZone's Guide to

Toggling Between Alternative Implementations Using Comments

·
Free Resource
If you have two implementations and are unsure which one's the keeper - this may just be what you need.
Imagine you are just working on some performance critical function/method and have to run some tests. Commenting out and changing stuff can be quite tedious if it involves lot's of code changes. With this snippet, you only have to delete (or add) one character to toggle between your options.
Here's how it's done.


...
//*
option1();
/*/
option2();
//*/
...
// option1() is used...


Delete the first / and the second option is used.


...
/*
option1();
/*/
option2();
//*/
...
// option2() is used...


If you re-add it, option 1 becomes active again.
So how is this done?
If you start with "//*" the line is a line comment ("*" is commented out).
"/*" starts a block comment instead. "/*/" is either an opening block comment or a closing block comment.
"//*/" is the end of a block comment when inside one - or it's a line comment commenting out "*/".
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}