Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Beginner
Oct 9, 2001



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Toggle Code Blocks with C Comments

If you find yourself constantly commenting and uncommenting bits of code during development, this tip could come in handy.

Normally, you'd do something like this:
#define /* #undef */ _FOO

bool x = true; // = false;

Instead, use C style commenting to your advantage:
/**/ #define /*/ #undef /**/ _FOO

bool x = /**/ true /*/ false /**/;

This defines _FOO and sets x to true. Deleting or adding the second slash toggles the comments:
/** #define /*/ #undef /**/ _FOO

bool x = /** true /*/ false /**/;

This also works when you have large chunks of code you wish to "toggle" and a preprocessor define would be overkill. Keep in mind this makes code harder to read to someone not familiar with the technique, so let your team know how it works and use it sparingly!
Paul Reynolds
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date