Using C++ debug visualizers in VS2015

No comments

Thursday, October 22, 2015

I have been using the .NET debug visualizers (and DebuggerDisplay atribute) for a long (long) time and I like the way they make my code easy(ier) to debug. Until recently I was not aware that Visual Studio has a similar capability for C++.
Any C++ dev armed with Visual Studio can use XML files to create debug visualizers, In Visual Studio 2015 there’s no longer a need to to deploy these visualizer on each on each developer machine – just add the new, easy to write visualizer to your project and your team gets to automatically use it via the magic of source control.
Consider the following class:

class Customer
{
 int m_id;
 string m_firstName;
 string m_surname;
 int m_age;
 Gender m_gender;
 string m_emailAddress;
 string m_telephoneNumber;
 Address m_address;

 // ...
};

In order to use a debug visualizer just add a new natvis file - Add new Item and choose the Debugger visualization file (.netvis) template.
image
And presto - a new debug visualization file would be added to your project.
After a little tinkering I’ve created the following:

<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
  <Type Name="Customer">
    <DisplayString>Name: {m_firstName}</DisplayString>
    <Expand>
    <Item Name="Id">m_id</Item>
    <Item Name="Gender">m_gender</Item>
    <Item Name="Age">m_age</Item>
    <Item Name="Address">m_address</Item>
    </Expand>
  </Type>
</AutoVisualizer>

Which was really simple to write:
  • To display the customer’s name automatically I’ve added DisplayString with “Name:” and the value in curly brackets.
  • Right now I only care about four of the customer’s fields so I’ve added Expends with those fields
And I got the following in my watch window:
image
Which is nice but not great – I would like to see the customer full name and so I’ve tweaked DisplayString a bit:

<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
  <Type Name="Customer">
    <DisplayString>Name: {m_firstName, sb} {m_surname, sb}</DisplayString>
    <Expand>
    <Item Name="Id">m_id</Item>
    <Item Name="Gender">m_gender</Item>
    <Item Name="Age">m_age</Item>
   <Item Name="Address">m_address</Item>
    </Expand>
  </Type>
</AutoVisualizer>

And I did it during a debug session – seeing the results each time I save the natvis file:
CppDebugVisulaizer1
I’ve used Format specifier (that’s the “sb”) to remove the quotes and make the name more human readable. In fact I can use another formatter for Gender – en (for Enum).
I would also like to make the customer address visible in the display string. Since Address is a class I’ll first define a visualizer for it (blow the customer visualizer) and update the customer visualizer to use the address as part of the display string:

<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
  <Type Name="Customer">
    <DisplayString>{m_firstName, sb} {m_surname, sb} [{m_address}]</DisplayString>
    <Expand>
      <Item Name="Id">m_id</Item>
      <Item Name="Gender">m_gender, en</Item>
      <Item Name="Age">m_age</Item>
      <Item Name="Address">m_address</Item>
    </Expand>
  </Type>
  <Type Name="Address">
    <DisplayString>{m_streetAddress, sb}, {m_city, sb}, {m_country, sb} Zip:{m_zipCode}</DisplayString>
  </Type>
</AutoVisualizer>

This doesn’t seem much – but in order to get similar behavior in .NET I’ve needed to use OzCode – since nested/recursive DebugDisplayAttribute is not supported in .NET.

Conclusion

There are other nifty debug visualizer related features in VS2015 but I find the ease of use and deployment to be the real game changer.
Now when I get to look at big vector of Customers without needed to expend and find what I want:
image
As simple as that.

Happy coding (Even in C++)…

What are you waiting for???

No comments

Sunday, October 04, 2015

The contract has been signed, the training finished and all the company is ready to start using the best development practices known to man - unit testing, TDD, Clean code, pair programming you name it! But there’s just one problem preventing the change from happening...
Or as the good doctor puts it:
You can get so confused that you’ll start in to race down long wiggled roads at a break-necking pace and grind on for miles across weirdish wild space, headed, I fear, toward a most useless place.
[Excerpt from Oh, The Places You’ll Go! by Dr. Seuss]
What could be this place which you ask? It’s the “we don’t have time now – let’s do it later” place.
I usually can see it coming a mile away. After initial work has been done, and the team is waiting to start the next stage/iteration. I get a talk from the team leader/manager/developer and he would tell me that xyz is a good idea, one which we would definitely use once we release the current version
I remember one developer tell me that he’s going to start using TDD when developing the next feature - over the course of a year (in which he implemented countless features).
Sometimes the team need the conditions to be just right – enough time (which you’ll never get), the right person to come back from vacation (and would have enough time between answering his emails and catching up with work?), some developers are waiting for permission (to do refactoring? or write unit tests - why not ask for permission to use integers instead?) - in this point I expect someone to tell me that he need to wait for the next moon eclipse so he can invoke the old gods and make sure that the new code would work.
But all these reasons (read: excuses) have one thing in common – they all make sure that we’re not leaving our warm and fuzzy comfort zone.
The Waiting Place…for people just waiting.
Waiting for a train to go or a bus to come, or a plane to go or the mail to come, or the rain to go or the phone to ring, or the snow to snow or waiting around for a Yes or No or waiting for their hair to grow. Everyone is just waiting.
Waiting for the fish to bite or waiting for wind to fly a kite or waiting around for Friday night or waiting, perhaps, for their Uncle Jake or a pot to boil, or a Better Break or a string of pearls, or a pair of pants or a wig with curls, or Another Chance. Everyone is just waiting.
[Excerpt from Oh, The Places You’ll Go! by Dr. Seuss]


And I’ve seen it happen countless times. It always comes down to – good idea, we defiantly need it, not now…
I really don’t understand the big deal with waiting – every single change in your code can be reverted (can has source control?) and you should be able to verify that your code works (if not make plans to fix that– quick). I’ve seen teams refuse to make a change that would reduce the amount of work they do (not to mention improve quality) and keep working twice as hard instead.64866203
Instead break the cycle – ask yourself: “how can I make sure that tomorrow I won’t make the mistakes I did today”. Try something new – you have absolutly nothing to loose.
No! That’s not for you!
Somehow you’ll escape all that waiting and staying. You’ll find the bright places where Boom Bands are playing. With banner flip-flapping, once more you’ll ride high! Ready for anything under the sky.
[Excerpt from Oh, The Places You’ll Go! by Dr. Seuss]


Happy coding…



Related Posts Plugin for WordPress, Blogger...