This blog has, IMO, some great resources. Unfortunately, some of those resources are becoming less relevant. I'm still blogging, learning tech and helping others...please find me at my new home on http://www.jameschambers.com/.

Tuesday, June 16, 2009

Toggle Between Code Blocks

Today I was trying to spike out a simple test where I was maxing out my network card and/or my CPU as fast quickly as possible.

Don’t ask.

Anyways, I had two blocks of code that I was working on, one to scan a subnet for ICMP-responsive devices, and another to blitz said devices with ping requests.  I needed to collect a bunch of IPs to ping so I didn’t fully flood any devices…then I was switching back to the block that did the pinging.  After using the Visual Studio buttons for commenting and removing comments for a while I remembered some c commenting bits I learned a while ago.

This is a really simple trick that an old friend of mine showed me back in the late 90’s (who now works at ohai).  The idea here is that you often find yourself working between two code blocks, but don’t want one to run while the other is, you’ll be forever commenting out one block of code and uncommenting the other.

In any language that supports the following two comment marks:

/*...*/  and //


…it is really easy to set up to comment out one block of code and work on the other.  Here’s the setup:



//*/
Console.WriteLine("Hello");
/*/
Console.WriteLine("Hello");
//*/


Now for the trick.  You’ll see that the top block is active and the bottom block is commented out.  To switch between blocks, simply comment out the first slash on the first line, as so:



/*/
Console.WriteLine("Hello");
/*/
Console.WriteLine("Hello");
//*/


…and Presto! you have activated the second block. 



I don’t normally run into this when I’m working in a TDD project, but I do hit it when I’m spiking a fair bit.  This isn’t really a game-changer or anything, but it is certainly handy when you’re fighting with a block of code or two.

2 comments:

  1. I have scratched together a code snippet for this.

    http://jonathan.dickinsons.co.za/blog/2009/12/code-block-toggler/

    Justice.

    ReplyDelete
  2. Nice, thanks for this Jonathan. I'm going to work this into my IDE tool bag.

    ReplyDelete