Click here to Skip to main content
11,636,300 members (74,694 online)
Click here to Skip to main content

Super-Easy Code Block Toggling

, 22 Aug 2011 CPOL 44.1K 18
Rate this:
Please Sign up or sign in to vote.
If you have two blocks of code that you are testing and need to switch back-and-forth between them, here's a commenting trick that works as a powerful code block toggling mechanism.Say you have a variable called foo of type string and want to change between using initialized values and...
If you have two blocks of code that you are testing and need to switch back-and-forth between them, here's a commenting trick that works as a powerful code block toggling mechanism.

Say you have a variable called foo of type string and want to change between using initialized values and non-initialized values for quick testing or more likely, because your boss asked you to. Silly boss!

//*
string foo = string.Empty;
/*/
string foo = "initialized";
//*/
foo += "bar";

Now, to toggle the section that is 'live', simply remove the first slash from the first line comment, as follows:

/*
string foo = string.Empty;
/*/
string foo = "initialized";
//*/
foo += "bar";

Obviously, for a simple string there are easier ways to do this, but this is sure handy for more complex object setup.

There you go! With one keystroke you totally own on switching between two blocks of code.

While #ifdef works in some scenarios this technique also works in non-compiled languages such as JavaScript.

Cheers!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

TheyCallMeMrJames
Software Developer (Senior)
Canada Canada
For almost 30 years I have been working with computers, learning a myriad of languages and participating in various computing environments.

Though I have been mentoring other developers for the last decade, I have recently found a strong interest in writing and am learning to translate the fun of in-person teaching to something I can get into article form.

I enjoy following technology trends, new gadgets and trying to guess where innovation will lead us next (I'm kinda holding out for a robot-served utopia, but willing to submit to our future robot leaders).

I am a guy who is passionate about my faith, my family and a cure for Juvenile Diabetes (my son lives with this disease).

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
nikhil _singh11-Jul-12 20:30
membernikhil _singh11-Jul-12 20:30 
GeneralAlthough its a nice trick, but its some times confusing and ... Pin
zenwalker198526-Oct-11 3:43
memberzenwalker198526-Oct-11 3:43 
GeneralReason for my vote of 5 very useful Pin
hs318012-Sep-11 5:19
memberhs318012-Sep-11 5:19 
GeneralNice (I have used a similar trick before)! Beware if there ... Pin
MrRossi29-Aug-11 21:52
memberMrRossi29-Aug-11 21:52 
GeneralReason for my vote of 2 nice, interesting trick Pin
Sheshadri Iyengar24-Aug-11 6:50
memberSheshadri Iyengar24-Aug-11 6:50 
GeneralReason for my vote of 5 Something Different...Like it!! Pin
GPUToaster23-Aug-11 23:02
memberGPUToaster23-Aug-11 23:02 
GeneralReason for my vote of 5 Good trick! Pin
Jose David Pujo23-Aug-11 2:35
memberJose David Pujo23-Aug-11 2:35 
GeneralReason for my vote of 5 So simple! Pin
Dave Cross22-Aug-11 22:42
memberDave Cross22-Aug-11 22:42 
GeneralReason for my vote of 5 Nice and sneaky. Pin
Walt Fair, Jr.22-Aug-11 12:48
subeditorWalt Fair, Jr.22-Aug-11 12:48 
GeneralReason for my vote of 5 Cute and clever! :) Pin
Gordon Brandly22-Aug-11 10:34
memberGordon Brandly22-Aug-11 10:34 
Reason for my vote of 5
Cute and clever! Smile | :)
GeneralReason for my vote of 4 Interesting :) Pin
Mohammad A Rahman21-Aug-11 22:50
memberMohammad A Rahman21-Aug-11 22:50 
GeneralInteresting! :) Pin
Mohammad A Rahman21-Aug-11 22:49
memberMohammad A Rahman21-Aug-11 22:49 
GeneralVery funny - I like it. But I use conditional compilation (#... Pin
johannesnestler17-Aug-11 23:37
memberjohannesnestler17-Aug-11 23:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150728.1 | Last Updated 22 Aug 2011
Article Copyright 2010 by TheyCallMeMrJames
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid