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.