ArticleslgStudy

computer science

Assertion (software development)

Assertion (software development) is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Assertion (software development) rather than just read about it. In short: 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 i…

Key takeaways

  • Assertion (software development) belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Assertion (software development) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Assertion (software development) from memory before moving on to harder problems.

Reference excerpt

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.

Worked examples

Example 1 — a first encounter with Assertion (software development)

Start with the simplest possible case. Write down what Assertion (software development) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Assertion (software development) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Assertion (software development) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Assertion (software development)

In research
Assertion (software development) appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Assertion (software development) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Assertion (software development) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Conditional constructs, Debugging, Formal methods, so understanding it makes those chapters shorter.
In everyday life
Look for Assertion (software development) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Assertion (software development)” →

How to study Assertion (software development) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Assertion (software development) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Assertion (software development) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Assertion (software development) in simple terms?

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 eva…

Why does Assertion (software development) matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Assertion (software development)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Assertion (software development).

Tags

  • Conditional constructs
  • Debugging
  • Formal methods
  • Logic in computer science
  • Software testing

Keep exploring