18B. Detecting, Diagnosing and Fixing Errors

Do your best, through careful planning and checking, to ensure that your code uses a correct algorithm and implements that algorithm correctly. An ounce of prevention is worth way more than a pound of cure.

But despite your best efforts, you will make mistakes. Everybody does. A major issue in software development is how to find mistakes and find the right way to fix them. For each mistake, there are a few logical stages in fixing it.

  1. Testing. If there is a mistake in your software, you want to know about it so that you can fix it. To mangle another adage, ignorance is far from bliss when your boss or your customer encounters the mistake instead. See more on testing.

  2. Localization. Usually, a mistake is in a small part of your software. Before you can find out what is wrong, you need to find out roughly where the mistake is.

  3. Diagnosis. Once you have discovered roughly where an error is, it is time to determine just what is wrong. Hand simulation and tracing can help with that.

  4. Fixing the error. If the error is the result of incorrectly coding a correct algorithm then, most of the time, once you see what the error is, the way to fix it is obvious.

    If the error is the result of an incorrect algorithm, it is back to planning. Do not try to patch the algorithm for a specific input. That just wastes your time. You will patch and patch and never reach something that works. Check the algorithm again, and be willing to abandon an incorrect algorithm in preference for a correct one.

That might look like a lot of work. For most mistakes, it is not. The worst approach that you can take is to avoid all of that and instead try a random change. If you are ever tempted to start trying changes without good reasons for believing that those changes are the right changes, then put your software down and come back to it later.


Exercises

  1. Why is it important to test software thoroughly? If I don't test it on a certain input, then I don't need to worry about whether the software works on that input, right? Answer

  2. What is error localization? Answer

  3. Why is it important to diagnose an error? Isn't it faster just to try some random changes? Answer