Sams Teach Yourself C++ in 21 Days

(singke) #1
One approach to this problem is to put a tryblock around the original call and catch
CancelDialogas an exception, which can be raised by the handler for the Cancel button.
This is safe and effective, but clicking Cancel is a routine circumstance, not an excep-
tional one.
This frequently becomes something of a religious argument, but a reasonable way to
decide the question is to ask the following: Does use of exceptions in this way make the
code easier or harder to understand? Are there fewer risks of errors and memory leaks, or
more? Will it be harder or easier to maintain this code? These decisions, like so many
others, require an analysis of the trade-offs; no single, obvious right answer exists.

A Word About Code Rot......................................................................................


Code rot is a well-known phenomenon in which software deteriorates due to being
neglected. A perfectly well-written, fully debugged program will turn bad on your cus-
tomer’s shelf just weeks after you deliver it. After a few months, your customer will
notice that a green mold has covered your logic, and many of your objects have begun to
flake apart.
Besides shipping your source code in air-tight containers, your only protection is to write
your programs so that when you go back to fix the spoilage, you can quickly and easily
identify where the problems are.

746 Day 20


Code rot is a programmer’s joke, which teaches an important lesson.
Programs are enormously complex, and bugs, errors, and mistakes can hide
for a long time before turning up. Protect yourself by writing easy-to-
maintain code.

NOTE

This means that your code must be written to be understood, and commented where
tricky. Six months after you deliver your code, you will read it with the eyes of a total
stranger, bewildered by how anyone could ever have written such convoluted and twisty
logic.

Bugs and Debugging............................................................................................


Nearly all modern development environments include one or more high-powered debug-
gers. The essential idea of using a debugger is this: You run the debugger, which loads
your source code, and then you run your program from within the debugger. This enables
you to see each instruction in your program as it executes and to examine your variables
as they change during the life of your program.
Free download pdf