- Step 1: Eliminate All Assumptions
- Step 2: Don't Be Afraid to Standardize
- Step 3: Live by the Golden Rule of Programming
- Step 4: Build Upon a Foundation of Smart Error Coding
Step 3: Live by the Golden Rule of Programming
Without complete error coding, reuse-quality code cannot be achieved. The Golden Rule is a simple three-part strategy that ensures that errors are dealt with as early as possible, both in the dimension of program logic and in that of the user experience:
-
Prevent all errors that can be anticipated and prevented.
-
Handle all errors that can be anticipated but not prevented.
-
Trap all errors that have not been anticipated.
According to the first rule, you must prevent all errors that can be anticipated and prevented. Examples include allowing the user to type too many characters into a field. That's something that can be anticipated and prevented.
The second mandate of the Golden Rule is that if you can anticipate but not prevent an error, you should handle it. Handling requires that you respond specifically to the error that occurred, taking any appropriate corrective action. Opening a file is an example of a process for which you can anticipate but not prevent errors.
The third rule says to trap any error you didn't anticipate. Obviously, if you can't anticipate an error, you can't handle or prevent it. The corollary to this rule is that you should trap an error only once. After that, you can anticipate it, so you must handle or prevent it.
Of course, there's no such thing as an error that can't be anticipated; there are only errors you haven't learned to anticipate yet. This is where experienced programmers earn their keep. They've been exposed to more errors, and should know how to anticipate more errors than new programmers do.
Following this Golden Rule will get you a long way toward reuse-quality programming.