Chapter 33. Ten Ways to Avoid Bugs

In This Chapter

  • Enable all compiler warning messages

  • Adopt a clear and consistent coding style

  • Comment your code while you write it

  • Single-step every path in the debugger at least once

  • Limit the visibility of members

  • Keep track of heap memory

  • Zero out pointers after deleting what they point to

  • Use exceptions to handle errors

  • Declare destructors virtual

  • Provide 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 sure that it understands what you want, it goes ahead and creates an executable file so you can ignore warnings if you like. In fact, if you really don't want to be bothered, you can disable ...

Get Beginning Programming with C++ For Dummies® 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.