Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Assertion (software development)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== 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 (programming language)|Java]] code: <syntaxhighlight lang="java"> int total = countNumberOfUsers(); if (total % 2 == 0) { // total is even } else { // total is odd and non-negative assert total % 2 == 1; } </syntaxhighlight> In [[Java (programming language)|Java]], <code>%</code> is the ''[[remainder]]'' operator (''[[Modulo operation|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 <code>total</code> is non-negative, so that the remainder of a division with 2 will always be 0 or 1. The assertion makes this assumption explicit: if <code>countNumberOfUsers</code> 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 pin-point the error without further debugging. Assertions are also sometimes placed at points the execution is not supposed to reach. For example, assertions could be placed at the <code>default</code> clause of the <code>switch</code> statement in languages such as [[C (programming language)|C]], [[C++]], and [[Java (programming language)|Java]]. Any case which 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 (programming language)|D]] such an assertion is added automatically when a <code>switch</code> statement doesn't contain a <code>default</code> clause. In [[Java (programming language)|Java]], assertions have been a part of the language since version 1.4. Assertion failures result in raising an <code>AssertionError</code> when the program is run with the appropriate flags, without which the assert statements are ignored. In [[C (programming language)|C]], they are added on by the standard header <code>[[assert.h]]</code> defining <code> assert (''assertion'') </code> as a macro that signals an error in the case of failure, usually terminating the program. In [[C++]], both <code>assert.h</code> and <code>cassert</code> headers provide the <code>assert</code> macro. 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.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)