12 General coding

 

This chapter covers

  • Exceptions that are not C++ exceptions
  • Proper loop setup and execution
  • Verbose coding and overuse of some keywords
  • Using deleted pointers

Improper design and implementation are not limited to C++. Many of the following mistakes are general and appear in many languages. This generality does not mean the following problems are unimportant, only that they are likely to occur in any code base. A language cannot be divorced from the architecture and machine on which it runs, although most languages attempt to abstract as many of those details as possible. Java’s approach eliminates many of the machine details but still shows signs of constraint. For example, bit sizes for data types limit the range of possible values—there appears to be no way around these machine details, regardless of attempts at abstraction.

C++ is closer to the machine than most languages. Therefore, it is no surprise that machine-specific details and problems crop up frequently. The following mistakes address several cases where language or machine details affect correctly implemented programs.

12.1 Mistake 91: Improperly handling division by zero

This mistake affects readability and effectiveness. Handling exceptions is a powerful technique for dealing with problems when recovery is possible and feasible.

12.2 Mistake 92: Incorrectly using the continue keyword in loops

12.3 Mistake 93: Failing to set deleted pointers to NULL

12.4 Mistake 94: Failing to return directly computed Boolean values

12.5 Mistake 95: Underusing expressions

12.6 Mistake 96: Using extraneous else keywords

12.7 Mistake 97: Not using helper functions

12.8 Mistake 98: Wrongly comparing floating-point values

12.9 Mistake 99: Floating-point to integer assignment

12.10 Mistake 100: Ignoring compiler warnings