Java Reference
In-Depth Information
This code is simpler and, therefore, better. Programs are complex enough without
adding unnecessary code.
The concept of assertions has become so popular among software practitioners
that many programming languages provide support for testing assertions. Java added
support for testing assertions starting with version 1.4 of the language. You can read
more about Java's assert statement in Appendix D.
Reasoning about Assertions
The focus on assertions comes out of a field of computer science known as program
verification.
Program Verification
A field of computer science that involves reasoning about the formal prop-
erties of programs to prove the correctness of a program.
For example, consider the properties of the simple if statement:
if (<test>) {
// test is always true here
...
}
You enter the body of the if statement only if the test is true, which is why you
know that the test must be true if that particular line is reached in program execution.
You can draw a similar conclusion about what is true in an if/else statement:
if (<test>) {
// test is always true here
...
} else {
// test is never true here
...
}
You can draw a similar conclusion about what is true inside the body of a while
loop:
while (<test>) {
// test is always true here
...
}
But in the case of the while loop, you can draw an even stronger conclusion. You
know that as long as the test evaluates to true , you'll keep going back into the loop.
 
Search WWH ::




Custom Search