In computer programming, specifically when using the imperative programming paradigm, an assertion is a predicate (a Boolean-valued function over the state space, usually expressed as a logical proposition using the variables of a program) connected to a point in the program, that always should evaluate to true at that point in code execution. Assertions can help a programmer read the code, help a compiler compile it, or help the program detect its own defects. For the latter, some programs check assertions by actually evaluating the predicate as they run. Then, if it is not in fact true – an assertion failure – the program considers itself to be broken and typically deliberately crashes or throws an assertion failure exception.
Details The following code contains two assertions, 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. For example: x > 0 { x++ } x > 1. The example above uses the notation for including assertions used by C. A. R. Hoare in his 1969 article. 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++:
The braces included in the comment help distinguish this use of a comment from other uses. Libraries may provide assertion features as well. For example, in C, the C standard library's <assert.h> header which provides the assert() macro.
Several modern programming languages include checked assertions – statements that are checked at runtime or sometimes statically. If an assertion evaluates to false at runtime, 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.
Usage In languages such as Eiffel, assertions form part of the design process; other languages, such as C and Java, use them only to check assumptions at runtime. In both cases, they can be checked for validity at runtime, but can usually also be suppressed.
Assertions in design by contract Assertions can function as a form of documentation: they can describe the state the code expects to find before it runs (its preconditions), and the state the code expects to result in when it is finished running (postconditions); they can also specify invariants of a class. Eiffel integrates such assertions into the language and automatically extracts them to document the class. This forms an important part of the method of design by contract. This approach is also useful in languages that do not explicitly support it: the advantage of using assertion statements rather than assertions in comments is that the program can check the assertions every time it runs; if the assertion no longer holds, an error can be reported. This prevents the code from getting out of sync with the assertions. For example, the following demonstrates design by contract in C++ (using C++26 contracts).
Assertions for run-time checking An assertion may be used to verify that an assumption made by the programmer during the implementation of the program remains valid when the program is executed. For example, consider the following Java code:
In Java, % is the remainder operator (modulo), and in Java, if its first operand is negative, the result can also be negative (unlike the modulo used in mathematics). Here, the programmer has assumed that total is non-negative, so that the remainder of a division by 2 will always be 0 or 1. The assertion makes this assumption explicit: if countNumberOfUsers() does return a negative value, the program may have a bug. A major advantage of this technique is that when an error does occur, it is detected immediately and directly, rather than later through often obscure effects. Since an assertion failure usually reports the code location, one can often pinpoint the error without further debugging. Assertions are also sometimes placed at points where the execution is not supposed to reach. For example, assertions could be placed at the default clause of the switch statement in languages such as C, C++, and Java. Any case that the programmer does not handle intentionally will raise an error, and the program will abort rather than silently continuing in an erroneous state. In D such an assertion is added automatically when a switch statement doesn't contain a default clause. In Java, assertions have been a part of the language since version 1.4. Assertion failures result in raising an java.lang.AssertionError when the program is run with the appropriate flags, without which the assert statements are ignored. In C, they are added on by the standard header <assert.h> defining assert(condition) as a macro that signals an error in the case of failure, usually terminating the program. In C++, this header is moved to <cassert>. The danger of assertions is that they may cause side effects either by changing memory data or by changing thread timing. Assertions should be implemented carefully, so they cause no side effects on program code. Assertion constructs in a language allow for easy test-driven development (TDD) without the use of a third-party library. In C#, there is no assertion macro or keyword, but instead classes System.Diagnostics.Debug and System.Diagnostics.Trace which provide Assert() methods. In Rust, there is an assert!() macro.
… excerpt ends here. Continue reading the full article.