Backend CodeTip Tools

CodeTip #4 Debugging with conditions & actions in Visual Studio

A long time ago I became a series of posts called CodeTip. The idea was simple, I wanted to give you quick but useful „programming lifehacks” which might help you in your daily work. Unfortunately, CodeTips died soon after the third episode and to be honest I completely forgot about them. Today I’ll do my best to resurrect it in a good, old way! Let’s start 馃槈

 

Sometimes it happens that knowing well some tool we suddenly discover an option that seems completely „exotic” but when we show it to the other developer all we hear is „wow, that’s nothing new… I knew it 2 years ago…”. And that’s exactly what happened to me a week ago. I had a problem with a production code which thrown some exceptions when the specific conditions have been met. My first and the most intuitive though was to debug the code. So, I toggled breakpoint in a chosen line and… suddenly I noticed that there was some „Settings” icon next to the red bubble:

 

icon

 

When I clicked that, my life changed completely forever. For all these years I had absolutely no idea that Visual Studio supports debugging with Conditions and Actions. So, if you’re surprised as I was read rest of this article 馃槈

 

Debugging with conditions

So, we’ve got three types of conditions that we can „bind” to the specific breakpoint:

  • Conditional expression (with params: Is true, When changed)
  • Hit count (with params: =, is multiple of, >=)
  • Filter (with param isTrue)

 

Since all names are self-explained we can move straight to some examples. Consider the following code:

 


class Program
{
    static void Main(string[] args)
    {
        var numbers = new List<int>();

        for(var i = 0; i < 100; ++ i)
        {
            if (i == 12)
            {
                numbers.Add(i);
            }                   

            Console.WriteLine(i); // we'll put the breakpoint here
        }
    }     
}


 

Let’s say that we’d like to hit the breakpoint when iterator (i) is even and greater than 34. For this purpose, we can use mentioned conditional expression. The syntax is equal to the C# one:

 

condition_1

 

When running the program, we can observe the result in runtime as follows:

 

result_1

 

Works like a charm! What’s even more interesting is the fact that we can also create conditional expressions based on the LINQ but you need to add proper using in the file (using System.Linq). Let’s say that we’d hit the breakpoint when numbers collection will contain an element greater than eleven. The expression is presented below:

 

condition_2

 

Now, we can run the program and check whether it’s working 馃檪

 

result_2

 

Another useful condition is based on the hit counter. Have you ever got an IndexOutOfRangeException while working with arrays? This might help you to stop the program in the proper iteration. Based on the previous code let’s create an exemplary condition:

 

condition_3

 

The condition works as expected (remember that we’ve started from 0):

 

result_3

 

Last but not least are filters which might help you debug code that uses several threads or processes (which usually is terrifying experience). The example with the result are given below:

 

condition_4

 

Debugging with Actions

Besides the conditions, Visual Studio also offers to execute an action when the specific breakpoint is reached. Default action suggested by the VS is logging a message to Output Window so聽聽this one might turn ou to be handy for developers who abuse Debug.WriteLine() in the production code 馃槈 Having a code from the previous paragraph we can add some action to the chosen breakpoint:

 

action

 

Below I put some of the Console’s information (which fortunately include our log from the action):

 

result_4

 

Knowing these options, your debugging sessions should be less painful in the future 馃檪