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:
“Even to this day it is easier than it ought to be for me to get a rise out of an American by telling him something about himself which is equally true about every human being on the face of the globe. He at once resents this as a disparagement and an assertion on my part that people in other parts of the globe are not like that, and are loftily superior to such weaknesses.”
—George Bernard Shaw (18561950)