There are generally three classes of programming errors:
·
Compilation Error (or Syntax Error): can be
fixed easily.
·
Runtime Error: program halts pre-maturely
without producing the results - can also be fixed easily.
·
Logical Error: program completes but produces
incorrect results. It is easy to detect if the program always produces wrong result.
It is extremely hard to fix if the program produces the correct result most of
the times, but incorrect result sometimes. For example,
// Can compile and execute, but give wrong result –
sometimes!
if (mark > 50) {
System.out.println("PASS");
} else {
System.out.println("FAIL");
}
This kind of errors is very serious if it is not caught
before production. Writing good programs helps in minimizing and detecting
these errors. A good testing strategy is needed to ascertain the correctness of
the program. Software testing is an advanced topics which is beyond our current
scope.
Debugging Programs
Here are the common debugging techniques:
·
Stare at the screen! Unfortunately, errors
usually won't pop-up even if you stare at it extremely hard.
·
Study the error messages! Do not close the
console when error occurs and pretending that everything is fine. This helps
most of the times.
·
Insert print statements at appropriate locations
to display the intermediate results. It works for simple toy program, but it is
neither effective nor efficient for complex program.
·
Use a graphic debugger. This is the most
effective means. Trace program execution step-by-step and watch the value of
variables and outputs.
·
Advanced tools such as profiler (needed for
checking memory leak and method usage).
·
Proper program testing to wipe out the logical
errors.
Testing Your Program for
Correctness
How to ensure that your program always produces correct
result, 100% of the times? It is impossible to try out all the possible outcomes,
even for a simple program. Program testing usually involves a set of
representative test cases, which are designed to catch the major classes of
errors. Program testing is beyond the scope of this writing.
No comments:
Post a Comment
Please write your view and suggestion....