DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Toggling Between Alternative Implementations Using Comments

11.22.2007
| 1486 views |
  • submit to reddit
        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 "*/".    

Comments

Marcel Akkerman replied on Fri, 2007/09/14 - 5:31am

Excellent post! May I ask how you figured this out? Did you just stumble upon it?