Chapter 33
Ten Ways to Avoid Bugs
In This Chapter
Adopting a clear and consistent coding style
Keeping track of heap memory
Using exceptions to handle errors
Providing a copy constructor and overloaded assignment operator
It’s an unfortunate fact that you will spend more time searching for and removing bugs than you will spend actually writing your programs in the first place. The suggestions in this section may help you minimize the number of errors you introduce into your programs to make programming a more enjoyable experience.
Enable All Warnings and Error Messages
The syntax of C++ allows for a lot of error-checking. When the compiler encounters a construct that it just can’t decipher, it has no choice but to output a message. It tries to sync back up with the source code (sometimes less than successfully), but it will not generate an executable. This forces the programmer to fix all error messages — she has no choice.
However, when C++ comes across a structure that it can figure out but the structure smells fishy anyway, C++ generates a warning message. Because C++ is pretty ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access