Expert C Programming

(Jeff_L) #1

all languages, not just C) is to code for debuggability. When you write the program, provide the
debugging hooks.


Handy Heuristic


Debugging Hooks


Did you know that most debuggers allow you to make function calls from the debugger
command line? This can be very useful if you have complicated data structures. Write and
compile a function to traverse the data structure and print it out. The function won't be
called anywhere in the code, but it will be part of the executable. It is a "debugger hook."


When you debug the code and you're stopped at a breakpoint you can easily check the
integrity of your data structures by manually issuing a call to your print routine. Obvious
once it's pointed out to you; not obvious if you've never seen it before.


We already hinted at coding for debuggability in the previous section, where we suggested coding an
FSM in two distinct phases: first do the state transitions, and only when they are working provide the
actions. Don't confuse incremental development with "debugging code into existence"—a technique
common among junior programmers, and those writing under too-strict time deadlines. Debugging
code into existence means writing a fast slapdash first attempt, and then getting it working by
successive refinements over a period of weeks by changing parts that don't work. Meanwhile, anyone
who relies on that system component can pull their hair out. "Sendmail" and "make" are two well
known programs that are pretty widely regarded as originally being debugged into existence. That's
why their command languages are so poorly thought out and difficult to learn. It's not just you—
everyone finds them troublesome.


Coding for debuggability means breaking the system down into parts, and getting the program
structure working first. Only when you have got the basic program working should you code the
complicated refinements, the performance tweaks, and the algorithm optimizations.


Handy Heuristic


Hash with Panache


Hashing is a way to speed up access to an element in a table of data. Instead of searching

Free download pdf