This page was saved using WebZIP 6.0.8.918 (Unregistered) on 01/20/05 오후 3:27:46.
Address: http://www.leepoint.net/notes-java/15principles_and_practices/40fail-early-fail-often.html
Title: Java: Fail Early, Fail Often  •  Size: 3970  •  Last Modified: Fri, 14 Jan 2005 00:25:13 GMT

Java: Fail Early, Fail Often

You will write bugs

In programming you will write a lot of bugs. Just accept it. I've known a lot of fantatically capable programmers, but only one who wrote essentially flawless code in the first draft, Ira Rubin.

Mistakes are educational

If you aren't writing bugs, at least while you're still learning the art of programming, you probably aren't pushing yourself to try new things. You can rationalize a lot of errors by repeating "I learn more from my mistakes than my successes", and it's probably true.

Compile-time errors are the best

Ideally, you want someone else to find your errors. Compiler error checking is your friend because it finds errors you've made immediately and tells you about them. Some development systems even have continuous compilation, so you are informed about errors during compilation. I haven't tried these systems, but imagine it might work something like spelling checkers that put a read line under words that might be errorneous as you type -- not intrusive, but letting you know where there may be a problem.

The quality of compile-time error diagnosis is both a function of language design and the compiler design. The Java language is so-so in this area, however its strong typing (variable types must be declared) and required casting are very good features. Some of the rumored featuers of JDK 1.5 will further improve compile time checks, eg, templates, type-safe enums, and a "foreach" statement. The goal of not allowing illegal programs to compile is impossible, but its always interesting to think of how the language design could be improved to help detect errors at compile time.

Catch runtime errors early if possible

The worst kind of runtime error causes the program to slowly fall apart. Perhaps the greated leap that Java made over C++ is in providing runtime checks that various common errors. Perhaps the most important features are garbage collection and array indexes out of range.

Yet, there are still many possible run-time errors. Java provides another great tool in the assert statement which checks that things that must be true really are (See Assertions). Learn about assertions and use them.

Rigorous testing

While it is true that testing can only show the presence of bugs, not their absence, it essential. Methodologies such as Extreme Programming require that test data be written before the code. The JUnit testing facility makes writing and running tests much easier. With fast machines it is possible to run regression tests frequently. See Testing.

Fail early, fail often

You will write bugs and you want them caught as early as possible. Use types to help catch errors at compile time if possible; use assertions to stop execution if something is wrong; use rigorous testing to find errors.