Code Focused

Test Out Code Variations at Runtime Using a Static Variable

Recompiling source code while debugging is possible, but sometimes your C++ project might not be configured to use it.

In this tip, I'd like to show a simple technique that allows switching between multiple versions of the code at runtime.

Often, when I'm testing a new solution, I'd like to run an old version and make a comparison, just to find out: Is my new method better or not? Sometimes it's even worthwhile to try several different approaches. In C++ apps this might not be super-easy because you need to recompile your project every time. But what if you could do it at runtime?

In Visual Studio there's an efficient option: Edit and Continue. While debugging, you can recompile the code and see changes while the app is running. While E&C is greatly improved in the more recent VS 2015, there are still some limitations (read more here). But in older versions of Visual Studio, for example, you aren't allowed to use the feature on 64-bit code. It's for those situations I'd like to show a simplified, "manual" version.

The approach might sound silly at first. But it works! Just add one static variable to the code. The variable could be a Boolean value or an integer, depending on your needs. Use it like this:

static bool bEnableMyNewFeature = true;  
  if (bEnableMyNewFeature) newSolution(); else oldSolution();  

Then, while debugging, you can change the value of this variable. Immediately you'll be able to see results, as shown in Figure 1. The variable must be static, so there's only one for all of the function's execution.

[Click on image for larger view.] Figure 1. Testing Out Code Variations at Runtime

How can you change the value during debugging? Go to the watch window or just hover on top of the variable. You'll see an edit box where the value can be changed.

I used this simple variable technique in places where I want to experiment with some options for new solutions. I can enable it, play with that, see if nothing breaks. The drawbacks: You have to know what code versions you want. The set of options must be well-defined. There's no way to write some new code while running the app (not without using Edit and Continue or some other advanced techniques).

The method I presented here might not work well in multithreaded code. There's a high chance not all threads will see the variable change immediately so that it might cause some additional problems. Still, for linear code, it should work relatively well.

Please remember to disable or, better, remove that ugly variable in the final builds and commits!

About the Author

Bartlomiej Filipek is a software developer in Poland who specializes in C++, Windows and graphics programming. He worked for a number of companies developing multimedia, document editors, games, graphics drivers and flight planning systems, and has taught game development at local university. Follow Bart's blog at http://www.bfilipek.com and on Twitter @fenbf.

comments powered by Disqus

Featured

  • Hands On: New VS Code Insiders Build Creates Web Page from Image in Seconds

    New Vision support with GitHub Copilot in the latest Visual Studio Code Insiders build takes a user-supplied mockup image and creates a web page from it in seconds, handling all the HTML and CSS.

  • Naive Bayes Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the naive Bayes regression technique, where the goal is to predict a single numeric value. Compared to other machine learning regression techniques, naive Bayes regression is usually less accurate, but is simple, easy to implement and customize, works on both large and small datasets, is highly interpretable, and doesn't require tuning any hyperparameters.

  • VS Code Copilot Previews New GPT-4o AI Code Completion Model

    The 4o upgrade includes additional training on more than 275,000 high-quality public repositories in over 30 popular programming languages, said Microsoft-owned GitHub, which created the original "AI pair programmer" years ago.

  • Microsoft's Rust Embrace Continues with Azure SDK Beta

    "Rust's strong type system and ownership model help prevent common programming errors such as null pointer dereferencing and buffer overflows, leading to more secure and stable code."

  • Xcode IDE from Microsoft Archrival Apple Gets Copilot AI

    Just after expanding the reach of its Copilot AI coding assistant to the open-source Eclipse IDE, Microsoft showcased how it's going even further, providing details about a preview version for the Xcode IDE from archrival Apple.

Subscribe on YouTube

Upcoming Training Events