Partager via


Comment/Uncomment code to switch versions quickly without using macros

In a typical day, I write or debug programs in several languages: typically Foxpro, C#, VB, C++ and 32 bit assembly, with an occasional MSIL, IDL and 64 bit ASM thrown in.

 

Sometimes, I like to switch between one version of code and another. This is useful if I want to do side by side comparisons of behavior.

 

One way to do this is with preprocessor macros, like this:

 

#If SomeValue

                <one version of code>

#else

                <another version>

#endif

 

However, that’s a fair amount of typing.

 

There’s a shortcut that works with C# and C++ style comments.

 

In these languages, a line that starts with “//” is a comment.

 

Also, a block comment (which can span multiple lines) starts with “/*” and ends with “*/”

 

 

//*

      int sub foo1() {

            int x = 2;

      Console.WriteLine((new System.Diagnostics.StackTrace().GetFrames()[0].GetMethod().Name)); // shows Foo1

      return x;

      }

/*/

      int sub foo2() {

            int x = 3;

      Console.WriteLine((new System.Diagnostics.StackTrace().GetFrames()[0].GetMethod().Name)); // shows Foo2

            return x;

      }

// */

 

With a single character change I can switch between foo1 and foo2: just delete the very first “/”. That changes the single line comment into a block comment. The “*/” of the “/*/” now acts like the end of the comment block.

Using an editor that colors the code (like Visual Studio) shows the switch properly

/*

      int sub foo1() {

            int x = 2;

      Console.WriteLine((new System.Diagnostics.StackTrace().GetFrames()[0].GetMethod().Name)); // shows Foo1

            return x;

      }

/*/

      int sub foo2() {

            int x = 3;

      Console.WriteLine((new System.Diagnostics.StackTrace().GetFrames()[0].GetMethod().Name)); // shows Foo2

            return x;

      }

// */

This technique is useful when creating sample code for others to play with, such as in my next blog post.

Comments

  • Anonymous
    March 30, 2009
    Bool a = false; //set breakpoint here if (a == true) //modify A to follow the code path you want.  {  code path 1  } else  {  code path 2  }

  • Anonymous
    March 30, 2009
    When I wrote my cartoon animation program almost 30 years ago (see Cartoon animation program ) I needed

  • Anonymous
    April 01, 2009
    Is it posssible to do something like this in vb?

  • Anonymous
    April 02, 2009
    For VB code, to switch between versions, I use something like this:                        If 0 Then                            Dim x = 2                        Else                            Dim x = 3                        End If Changing the 0 to a 1 makes it reverse the code

  • Anonymous
    May 28, 2009
    In the last post, Area fill algorithm: crayons and coloring book , I showed a program that emulates a

  • Anonymous
    May 28, 2009
    PingBack from http://www.codedstyle.com/stack-overflow-expand-your-stack-change-your-algorithm-10/

  • Anonymous
    May 28, 2009
    PingBack from http://www.codedstyle.com/stack-overflow-expand-your-stack-change-your-algorithm-15/