Java Reference
In-Depth Information
Code 12.16
continued
Using assertions for
internal consistency
checks
public
void
removeDetails(String key)
{
if
(key ==
null
) {
throw new
IllegalArgumentException(
"Null key passed to removeDetails."
);
}
if
(keyInUse(key)) {
ContactDetails details = book.get(key);
book.remove(details.getName());
book.remove(details.getPhone());
numberOfEntries--;
}
assert
!keyInUse(key);
assert
consistentSize() :
"Inconsistent book size in removeDetails"
;
}
The
assert
keyword is followed by a boolean expression. The purpose of the statement is to
assert something that should be true at this point in the method. For instance, the first assert
statement in Code 12.16 asserts that
keyInUse
should return
false
at that point, either
because the key wasn't in use in the first place or because it is no longer in use as the associ-
ated details have now been removed from the address book. This seemingly obvious assertion
is more important than might at first appear; notice that the removal process does not actually
involve use of the key with the address book.
Concept:
An
assertion
is a
statement of a fact
that should be true
in normal program
execution. We can
use assertions to
state our assump-
tions explicitly and
to detect program-
ming errors more
easily.
Thus, an assert statement serves two purposes. It expresses explicitly what we assume to be true
at a given point in the execution and therefore increases readability both for the current devel-
oper and for a future maintenance programmer; also, it actually performs the check at runtime
so that we get notified if our assumption turns out to be incorrect. This can greatly help in find-
ing errors early and easily.
If the boolean expression in an assert statement evaluates to
true
, then the assert statement
has no further effect. If the statement evaluates to
false
, then an
AssertionError
will be
thrown. This is a subclass of
Error
(see Figure 12.1) and is part of the hierarchy regarded as
representing unrecoverable errors—hence, no handler should be provided in clients.
The second assert statement in Code 12.16 illustrates the alternative form of assert statement. The
string following the colon symbol will be passed to the constructor of
AssertionError
to provide
a diagnostic string. The second expression does not have to be an explicit string; any value-giving
expression is acceptable and will be turned into a
String
before being passed to the constructor.
The first assert statement shows that an assertion will often make use of an existing method
within the class (e.g.,
keyInUse
). The second example illustrates that it might be useful to pro-
vide a method specifically for the purpose of performing an assertion test (
consistentSize
in this example). This might be used if the check involves significant computation. Code 12.17
shows the
consistentSize
method, whose purpose is to ensure that the
numberOfEntries
field accurately represents the number of unique details in the address book.
Search WWH ::
Custom Search