Assertion (computing)
In computer programming, an assertion is a predicate (a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true at that place.
For example, the following code contains two assertions: x: positive integer
x := 5; {x > 0} x := x + 1 {x > 1}x > 0
and x > 1
, and they are indeed true at the indicated points during execution.
Programmers can use assertions to help specify programs and to reason about program correctness. For example, a precondition — an assertion placed at the beginning of a section of code — determines the set of states under which the programmer expects the code to execute. A postcondition — placed at the end — describes the expected state at the end of execution.
The example above uses the notation for including assertions used by C.A.R. Hoare in his 1969 paper. That notation cannot be used in existing mainstream programming languages. However, programmers can include unchecked assertions using the comment feature of their programming language. For example, in C:
x = 5; x = x + 1; // {x > 1}The braces included in the comment help distinguish this use of a comment from other uses.
Several modern programming languages include checked assertions - statements that are checked at runtime or sometimes statically. If an assertion evaluates to false at run-time, an assertion failure results, which typically causes execution to abort. This draws attention to the location at which the logical inconsistency is detected and can be preferable to the behaviour that would otherwise result.
The use of assertions helps the programmer design, develop, and reason about a program.
Read more about Assertion (computing): Usage, Disabling Assertions, Comparison With Error Handling, Use in Computer Circuits, History
Famous quotes containing the word assertion:
“I feel a kind of reverence for the first books of young authors. There is so much aspiration in them, so much audacious hope and trembling fear, so much of the hearts history, that all errors and shortcomings are for a while lost sight of in the amiable self assertion of youth.”
—Henry Wadsworth Longfellow (18071882)