Assertions in Software Engineering
In COMPUTER-SCIENCE and SOFTWARE-ENGINEERING, an ASSERTION is a statement that a predicate is expected to be true at a specific point during program execution. If the predicate evaluates to false, the program typically terminates or throws an error, indicating a bug in the logic. Assertions are a fundamental tool for DEBUGGING and maintaining code quality.
The concept is a core component of DESIGN-BY-CONTRACT, a methodology pioneered by BERTRAND-MEYER. In this context, assertions are used to define preconditions, postconditions, and class INVARIANT definitions. By explicitly stating these assumptions, developers can ensure that the SOURCE-CODE behaves as intended during the development cycle.
Most modern programming languages provide built-in support for assertions. In the C-PROGRAMMING-LANGUAGE, the assert.h header provides a macro to check expressions at runtime. In JAVA, the assert keyword was introduced to facilitate internal testing, as detailed in the Oracle Java Documentation. Similarly, PYTHON utilizes an assert statement for internal consistency checks; however, as noted in the Python Software Foundation documentation, these can be optimized away when the interpreter is run with optimization flags.
Unlike error handling mechanisms such as EXCEPTION-HANDLING, assertions are generally not intended to handle recoverable runtime errors like missing files or network timeouts. Instead, they catch developer errors that should never occur in a correctly functioning system. For further reading on implementation strategies, see the Wikipedia overview of Assertions.