O'Reilly logo

Safe C++ by Vladimir Kushnir

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 16. Making Your Code Debugger-Friendly

Have you ever tried to look inside some object in the debugger and been frustrated that the debugger shows the details of the object’s physical implementation instead of the logical information that the object is supposed to represent? Let me illustrate this using an example of a Date class that represents calendar dates, such as December 26, 2011. If you look into this object in the debugger, chances are you will not see anything resembling “December 26, 2011” or any human-readable information at all, but rather an integer that requires some decoding to convert into a date it represents.

It all depends on how the Date type is implemented. I have seen the following three implementations:

  1. class Date {
      // some code
    
     private:
      int day_, month_, year_;
  2. typedef Date int; // in YYYYMMDD format
  3. class Date {
      // some code
    
     private:
      int number_of_days_; //  Number of calendar days since the "anchor date"

The first implementation is pretty self-evident and is a pleasure to debug. In the second case, the date December 26, 2011 is represented by an integer 20111226, which is also easily readable by a human once you know the formula behind it.

In the last case, the internal representation of a Date is the number of days that have passed since some arbitrarily chosen date far enough in the past, that the day represented by 1 is 1/1/1900 or 1/1/0000 or something of this sort.

While the first two implementations are very debugger-friendly, they have a serious ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required