|
March 25, 2001 Defensive Programming Tips: March 2001
Yehuda Shiran, Ph.D.
|
|
Defensive programming is a technique
you can use to improve the robustness of your programming. You need to think about
possible errors from your users. Don't assume anything. The only sure thing is
that if something can go wrong, it will. It's similar to taking a defensive driving
course. One way to prevent accidents is to think ahead and be aware of potential
problems. It's the same in programming. You could write the most brilliant algorithm,
but your users may not know how to use it, or, even worse, use it incorrectly.
Let's look at an example. The following code includes a mistake. Can you find
it?
You guessed it right. The code is missing the keyword
Try calling init(). You will never get an error, because we create the object for you, even if the calling code doesn't.
People who read this tip also read these tips: Look for similar tips by subject: |