Language - Assertion

Card Puncher Data Processing

About

In computer programming, an assertion is a predicate (for example a true–false statement) placed in a program to indicate that the developer thinks that the predicate is always true or false at that place.

An assertions is an executable check for a Boolean expression that must be true.

Assertions contain Boolean expressions that define the correct state of your program at specific points in the program source code.

An assertion has a Boolean expression that, if evaluated as false, indicates a bug in the code. This mechanism provides a way to detect when a program starts falling into an inconsistent state.

Assertions are excellent for documenting assumptions.

Assertions helps developers write code that is more correct, more readable, and easier to maintain.

You can think of assertions as a uniform mechanism that replaces the use of ad hoc conditional tests.

Assertions are used to document (logically impossible|fundamental) situations.

Assertions play an important role in debugging and designing code with testability and design-by-contract style of programming in mind.

Assertions do not:

  • allow for recovery from errors as error handling does (an assertion failure halt the program's execution abruptly)
  • display a user-friendly error message.

This code is absolutely loaded with assertions and these are permanently enabled

Julian Seward, Valgrind

It's acceptable to spend 5% of the total running time doing assertions checks.

Why Assertions

  • make code self checking
  • make code fail early (closer to the bug)
  • assignment blame
  • document assumptions,
    • preconditions,
    • postconditions
    • invariants

Rules

  • Assertions are not for error handling
  • No Side effects
  • No Silly assertions

Example

BankAccount acct = null;

// ...
// Get a BankAccount object
// ...

// Check to ensure we have one
             
assert acct != null;

This asserts that acct is not null. If acct is null, an AssertionError is thrown. Any line that executes after the assert statement can safely assume that acct is not null.

In java:

  • to turn assertions off, you need to recompile the code.
  • the important thing to note about the assertion facility is that it is not provided as a user-level class library. Rather, it is built into the language by introducing a new keyword statement to Java technology: assert.

Assertions vs error handling

Although the use of assertions replaces the ad hoc use of conditional tests with a uniform methodology, it does not allow for a repair strategy (as error handling does) to continue program execution.

For instance, the error handling Java exception mechanism with the use of try/catch/finally allows you to process the case instead of aborting the program as in assertions.

This means that when an exception is detected, the program aborts with no recovery mechanism.

How to

Remove assertion

With assertions, the program will be larger in size and therefore slower to load. When testing and debugging is completed, assertions do not have to be removed from the program.

Assertions can be compiled out in order to ignore all assertions, instead of deleting them manually.

Library

  • Java: com.google.common.base.PreConditions

Documentation / Reference





Discover More
Card Puncher Data Processing
Application - Design By Contract model

A Design By Contract (DBC) model is based on the fact that a computation, given correct input, must terminated with a correct output. This assertion notion is central in a DBC design. If any assertion...
Testing Infrastructure
Code Test - Assertion Library

in a code testing context. assertiontest method onlytest framework . An assertion library helps you write your result expectation of a code in a unit test between: the value that you get from your...
Java Conceptuel Diagram
Java - Assert

The assert keyword in Java is a inline assertion. Its first role is to reduce the following code: by this: If the assertion fails, you get a java.lang.AssertionError: where: Expression1 is...
Eclipse Oepe Junit Library
Java - JUnit

Written by Erich Gamma (of Design Patterns fame) and Kent Beck (creator of XP methodology) A simple framework to write and run repeatable tests JUnit features include: Assertions for testing expected...
Regexp
Regexp - Assertion (Condition)

An assertion is an assertion that specifies a condition that has to be met at a particular point in a match, without consuming any characters from the subject string. simple assertion designed...
Security - Identity Assertions

Identity / Security An identity Authentication use as credentials: certificates or .



Share this page:
Follow us:
Task Runner