kolenda kolenda - 1 year ago 71
C Question

Simple ways to disable parts of code

This isn't a typical question to solve a specific problem, it's rather a brain exercise, but I wonder if anyone has a solution.

In development we often need to disable or switch some parts of code to check different approaches. To do this we use comments or

, but my favorite is:

[code here]

Now when you remove just the first slash the code will become commented-out.

The question: is there any way to implement similar if-else code switch? I tried to find it but I always had some problem and couldn't find a working solution.

And maybe do you know any similar tricks?

Answer Source

I'm not sure I should post this because it's not something I think is 'good code', but I'll admit to having used the following technique as a quick-n-dirty way to be able to quickly switch between two small snippets of code when I'm just checking something out:

// in the following , foo() is active:
/**/ foo(); /*/ bar(); /**/

Now just remove one of the asterisks at the front:

// now bar() is active:
/*/ foo(); /*/ bar(); /**/

Of course, this should never make it past the "just checking things out" phase...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download