Toggling Between Alternative Implementations Using Comments
Join the DZone community and get the full member experience.Join For Free
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 "*/".
Opinions expressed by DZone contributors are their own.