Chapter 28. Ten Ways to Avoid Adding Bugs to Your Program

In This Chapter

  • Enabling all warnings and error messages

  • Using a clear and consistent coding style

  • Limiting the visibility

  • Adding comments to your code while you write it

  • Single-stepping every path at least once

  • Avoiding overloaded operators

  • Heap handling

  • Using exceptions to handle errors

  • Use static assertions

  • Avoiding multiple inheritance

In this chapter, I look at several ways to minimize errors, as well as ways to make debugging the errors that are introduced easier.

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 can't decipher, it has no choice but to generate an error message. Although the compiler attempts to sync back up with the next statement, it does not attempt to generate an executable program.

Disabling warning and error messages is a bit like unplugging the Check Engine light on your car dashboard because it bothers you: Ignoring the problem doesn't make it go away. If your compiler has a Syntax Check from Hell mode, enable it.

Don't start debugging your code until you remove or at least understand all warnings generated during compilation. Enabling all warning messages if you then ignore them does you no good. If you don't understand the warning, look it up. What you don't know will hurt you.

Adopt a Clear and Consistent Coding Style

Coding in a clear and consistent style not only enhances the readability of your program but also ...

Get C++ For Dummies®, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.