Level 3: If there is a bug, the program will work incorrectly. In this case, there is no error message to warn you of the problem. The program simply behaves in some way that is incorrect.
This is worse for a couple of reasons:
- First, the behavior might be incorrect in some subtle, non-obvious way. A large amount of careful testing and analysis might be required to detect the bug. In the worst case, you might not catch it at all, and only learn of its existence when a customer reports it to you.
- Second, even when you learn of the existence of a bug, it may not be obvious what is causing it. This is especially serious when you do not know a reliable way to reproduce it. A long, painful debugging process may be required before you finally track down the cause of the problem and fix it.
Suppose you are writing a method to perform some operation, and realize that in certain cases, it is impossible to do exactly what you intend. For example, suppose it is meant to set the value of some parameter. That value is stored both in memory and on disk, and the method is supposed to update it in both places.
What if an error prevents you from updating the value on disk, perhaps because another thread already has the file open for writing? There are two obvious choices for what to do:
- Update the value only in memory, then return.
- Throw an IOException and force whatever code is invoking the method to handle it.
Inexperienced programmers will often choose the first approach. They will claim this is "more robust", since the method partly succeeds instead of completely failing. They will say they are making the code "fault tolerant". They will say it produces an easier to use API, since programmers invoking the method are not forced to worry about IOExceptions. They are, however, completely wrong.
In fact, what they are doing is replacing a level 2 bug opportunity -- an exception that clearly tells you what has happened-with a subtle, hard to identify source of intermittent bugs. Every method should precisely define what its behavior is. If it cannot do exactly what is expected, it should signal this in a way that alerts either the programmer or the user to the problem.
A level 3 bug opportunity is bad, and you should try hard to avoid them, but there is something even worse: