Defensive Programming is the use of programming techniques to prevent problems before they occur.
Defects are errors in the software itself. A fault is a problem that arises from a defect. Some defects never cause faults (attributed to defects in testing!). All faults are caused from at least one defect. "Bug" is common synonym for defect.
The term "computer bug" is usually attributed to Admiral Grace Hopper of the US Navy, who worked on some of the early computers, found the first "actual bug in computer" in 1945. A moth crawled inside a electro-mechanical relay in a computer and caused a short-circuit. The term "bug" was also used in 1896 for electrical circult problems.
Instrumentation is little bits of code added to a program to get a better understanding of how it behaves.
You've just started your first job at Commerical Bank. You've been a given a simple account processing program to fix. See Account.cs.
There are a number of defects in the program.
First, calculate the expected balance by hand (or with a calculator) by reading the list of deposit() and withdrawl() calls at the end of the program.
Compile and run the program to compare the expected balance with the result of the program.
To figure out how the program works you will "instrument" the program so you can see what parts of the program are used.
A simple instrumentation might be to print the name of the class and method and the arguments and result value at the beginning and the end of the method body.
publc class YourClass
{
public static double aMethod(double anArgument)
{
double result;
result = anArgument + 123.45;
return result;
}
}
public class YourClass
{
public static double aMethod(double anArgument)
{
double result;
Console.WriteLine("DEBUG: METHOD: BEGIN: aMethod({0})", anArgument);
result = anArgument + 123.45;
Console.WriteLine("DEBUG: METHOD: END: aMethod({0}) => {1}", anArgument, result);
return result;
}
}
See Account2.cs for an instrumented version of Account.cs.
Make a Debug helper class to print the method instrumentation for you.
public class Debug
{
public static void methodBegin(object class, string method, object arg1)
{
Console.WriteLine("DEBUG: METHOD: BEGIN {0}.{1}({2})", class, method, arg1);
}
public static void methodEnd(object class, string method, object result)
{
Console.WriteLine("DEBUG: METHOD: END {0}.{1}(...) => {2}", class, method, result);
}
}
public class YourClass
{
public static double aMethod(double anArgument)
{
double result;
Debug.methodBegin("YourClass", "aMethod", anArgument);
result = anArgument + 123.45;
Debug.methodEnd("YourClass", "aMethod", result);
return result;
}
}
Interface defects occur when one part of a program uses another part incorrectly.
The Account.cs program has (at least) 5 interface defects. Can you find them?
An assertion is a short test in a program that validates that the program is doing what was intended and designed for.
Conditions that must be true at the beginning of a function.
public static double divide(double x, double y)
{
double result;
Debug.assert("y != 0", y != 0);
result = x / y;
return result;
}
See Account4.cs for an example with assertions added.
Conditions that must be true at the end of a function.
What assertions (pre-conditions) should be added to prevent the interface defects?
Make a Debug helper class to instrument and handle assertions.
Programming Pearls, Jon Bentley, Addison-Westley, ISBN 0-201-65788-0
More Programming Perls, Jon Bentley, ISBN: 0-201-11889-0
The Practice of Programming, Brian W. Kernighan, Rob Pike, ISBN 0-201-61586-X
http://www.jamesshuggins.com/h/tek1/first_computer_bug.htm
http://www.history.navy.mil/photos/pers-us/uspers-h/g-hoppr.htm
http://www.geocities.com/xtremetesting/TestingDictionary.html
http://www.it.nuigalway.ie/~j_duggan/courses/ct216/CT216-Testing/
http://hissa.nist.gov/latex/node10.html