Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Switch betweeen method versions

I need to rewrite some c++ classes and I would like to be able to switch between the new code and the old code (reliable and fast). What would be good options to do so? In general I need some kind of switch that decides what to do, like:

int foo::bar()
{
   if (yesUseNew == true)
  {
    return 1 + 1;
  }
  else
  {
    return 1 + 2;
  }
}

HOW and where could i set yesUseNew? It should be useable in Debug-Build and in Release and it should be applied directly. So reading some xml-Config would be to late.

The attempt, with two code-versions directly in the methods, is only an example. At this point I am not sure on which abstraction level I will do this. The primary question is actually HOW I can distinguish between the versions (fast).

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks!

>Solution :

Well, I see the following alternatives :

  1. At compile time with #define

e.g:

#define V1  //Comment this line if you want V2. You can also define it on the command line of your compiler

void myfunction() {
#ifdef V1
//V1 Implementation
#else
//V2 Impl
#endif
  1. At runtime with env variables for example

This means you can switch without recompiling.

if (std::getenv("V1"))
    //V1 Code
else
    //V2 Code
  1. Add a configuration option to your app, either in the command line or the config file if it has one. If the dual behaviour will last this is the way to go
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading