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 */ _FOObool x = true; // = false;

Instead, use C style commenting to your advantage:

 /**/ #define /*/ #undef /**/ _FOObool x = /**/ true /*/ false /**/;

This defines _FOO and sets x to true. Deleting or adding the second slash toggles the comments:

 /** #define /*/ #undef /**/ _FOObool 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!

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: