SLIDE 1
1
1
Assertions
2
Assertions
- assertions communicate assumptions about the
state of the program, and stop processing if they turn out to be false
- very often comments are statements about the state
the program should be in
- but assertions can tell the run-time system what the
programmer believes is going on – if these assumptions turn out not to be the case, the run-time system can stop processing
- Java assert uses exceptions as its mechanisms
3
Assertions (cont.)
- assertion checking can be switched on and off as
required
- assertions can be used during the test phase for a
product, and then be switched off for production deployment
- assertions may be seen as an extension to the safety
features provided by the language – built-in checks ascertain the validity of primitive
- perations, say, array indexing, casts, etc.
– programmer-written checks do the same at a higher level for user-defined abstractions, e.g., new data structures
4
Assertions (cont.)
- seeing checks as a safety system, you may not want
to turn them off (completely)
- turning on checking only during development and
testing is sometimes described as: "wearing a life jacket when close to shore, and throwing it overboard once you are in the middle
- f the ocean"