I had to use a conditional break point recently, and I was very thankful for that feature. I thought it might be worth a quick post to spread the communal joy of conditional break points.
So, I’m assuming with the ideas of a break point. That is, clicking on the bar next to your code and adding a little red dot that will stop your code running (if your in debug mode) and let you take a closer look at what is going on. That’s a really useful feature.
Now suppose that you’re loading a file with 10,000 or so lines, and everything works as you expect it to for 4,000 lines, but then things start going wrong. Here’s an example of what the code might look like:
string name, addressLine1, addressLine2, starSign; uint age; StreamReader reader = new StreamReader(@"D:\Files\files.txt"); while(!reader.EndOfStream) { name = reader.ReadLine(); addressLine1 = reader.ReadLine(); addressLine2 = reader.ReadLine(); starSign = reader.ReadLine(); if (!uint.TryParse(reader.ReadLine(), out age)) { Console.WriteLine("Error parsing age value"); } } reader.Close();
It would be really useful if you could stop the code at that point, and take a look at the data when it starts to go wrong, but if you slap a break point inside that while loop it’s going to get hit 4,000 times before you get to where you want to be.
This is a perfect example where you could use a conditional break point. If you right click on the break point you are presented with a menu of options.
In this case, you could select Hit Count and the specify when you want the code to break it terms of how many times this breakpoint has been hit.
Another way of thinking of it would be that you want the break point to remain inactive for the first 3,999 times the code passes it, and then you want the code to stop so you can step through and see what is going on in more detail.
That’s really useful! but what are some of those other options? Might they be just as useful?
Well yeah, they are. Condition allows you to type a condition to break on, should it ever happen. In this case the code will break if the name read in is “Steve”.
When Hit allows you to instruct the compiler to do other things when the breakpoint is hit, such as print a message to the Output pane in visual studio – that means no more writing out to the console, assuming that there is a console to write out to!
You can even combine different breakpoints, so for example I could write the addressLine1 value for all the entries with the name “Steve” to visual studios Output pane, it I wanted to:
It’s a very useful feature and if you’re not familiar with it already you should definitely check it out!
Using the Task List
To do lists are wonderful things. When you have a lot of things to do they help you to organise and prioritise all those things, they give you satisfaction when you cross something off your list, and they give you feedback on how you’re doing and whether you need to put some things on the back burner for a while.
Visual studio offers a built in task list to which you can add user tasks, assign priorities and tick off completed tasks.
User Task List – I wish my real to do list was this empty 😦
There’s more though, the Task List will also scan your code for token comments – which are special comments containing specific strings.
By looking at the comments section you can see a TODO comment and a HACK comment that have been picked up automatically by the task list which read the comments in the code. Once the task is done the comment is deleted.
These tasks have been generated by reading comments in the code
You can even specify your own tokens. They say that you shouldn’t worry about making code more efficient until efficiency actually becomes a problem.
Adding a new INEFFICIENT token – it’s low priority though because efficiency isn’t an issue yet
You can add your own tokens by selecting Tools ->Options -> Environment -> Task List. Here I’ve created an INEFFICIENT token, so that if I write code that I know could be more efficient I can give it the INEFFICIENT token, and if I have to look for ways to speed up code later I can look for all the pieces of code that have been given this token.
Now if I ever want to speed up my code I’ll know just where to look!