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:
class Date { // some code private: int day_, month_, year_;
typedef Date int; // in YYYYMMDD format
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 ...
Get Safe C++ now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.