466 13.6 Building debugging infrastructure
Modern programming languages have few uses for the goto statement.
C and C++ don't have any means to exit from inner loops other than a
goto. If you need to implement high-level control structures, such as finite-
state machines or decision tables, you will need to code them using a goto.
Some constructs should be avoided because they're proven error
generators.
1 3.5.2.1 9 Language-construct avoidance examples
Listed as follows is a set of conventions for C++ that address the issues of
construct avoidance. There are other choices that are reasonable and effec-
tive in preventing bugs.
9 Don't use
goto
except to break out of loops that are multiply nested.
9 Don't use malloc, realloc, or flee.
9 Don't assume that an
int
is thirty-two bits wide.
9 Don't assume that pointers and integers are the same width.
9 Do use inline functions instead of #define with arguments.
9 Do use enumerations instead of #define for groups of named constants.
9 Do use const instead of #delqne for individual named constants.
9 Do use typedef to simplify declaration of function pointers.
9 Do use the preprocessor only to compile code conditionally.
Java is a recent language without the long-term legacy of features that C
or C++ have. We don't typically find lists of features to avoid in Java pro-
gramming standards.
1 3.6 Building debugging infrastructure
13.6.1
Augmented data structures
If you know that you will have to debug a program you're developing, con-
sider augmenting your data structures with information that will make the
debugging task easier. Several different types of data can be added.
One way to augment data structures is to provide identification informa-
tion that connects the data structure to the user input or the outside world.
13.6 Building debugging infrastructure 467
1 3.6.2
Compiler developers normally insert the source-file name, line number, and
column number into the internal representations the compiler builds while
parsing a program. This enables the compiler to generate error messages that
relate back to the element of the user program that is erroneous.
Optimizing compilers normally use additional representations, such as
control-flow graphs, derived from the representation generated during pars-
ing. The source-file, line, and column information must be consistently
transferred to these intermediate representations. When a problem in the
compiler occurs, the compiler writer can more easily trace the problem in
his data structures to the user program and often create a minimal test case.
Programs that process discrete transactions can include timestamp
information in data structures that are related to the transactions. Time-
stamps can be collected in several ways. Sometimes the most valuable
information is the time of the original transaction. In other situations, it's
useful to collect a string of timestamps that identify each occasion when a
data structure was updated. In other situations, just the time of the most
recent update is useful.
Another way to augment data structures is to provide structural redun-
dancy. Linked lists and B-trees can be augmented to include redundant
structural information. Research into augmented data structures was moti-
vated by the desire to build fault tolerance into software. The bibliography
contains several references on this subject.
When data structures have been afflicted by memory corruption, debug-
ging can be very difficult. Data structures that contain sufficient informa-
tion to detect corruption, and even to correct the problem automatically,
make this task much easier. Designers of such robust structures take care to
minimize both the extra space and extra time required to use them.
Augmented procedures
If you know that you will have to debug a program you're developing, con-
sider augmenting your procedures with code you can use to help with
debugging. Chapter 9 begins with the following tactics that involved aug-
menting procedures:
9 Display variable values.
9 Display execution messages.
9 Display procedure arguments.
I Chapter 13

Get Debugging by Thinking 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.