Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are assertions? and why would you use them?

Tags:

c++

assert

How are assertions done in c++? Example code is appreciated.

like image 206
yesraaj Avatar asked Oct 31 '08 11:10

yesraaj


People also ask

Why do you use assertions?

Assertions are used to codify the requirements that render a program correct or not by testing conditions (Boolean expressions) for true values, and notifying the developer when such conditions are false. Using assertions can greatly increase your confidence in the correctness of your code.

What are assertions explain?

: insistent and positive affirming, maintaining, or defending (as of a right or attribute) an assertion of ownership/innocence. : a declaration that something is the case.

What are programming assertions What is their purpose?

An assertion is a statement in the Java programming language that enables you to test your assumptions about your program. For example, if you write a method that calculates the speed of a particle, you might assert that the calculated speed is less than the speed of light.

In which cases are assertions useful?

Assertions are mainly for debugging. They'll help you ensure that you don't introduce new bugs while adding features and fixing other bugs in your code. However, they can have other interesting use cases within your development process. These use cases include documenting and testing your code.


2 Answers

Asserts are a way of explicitly checking the assumptions that your code makes, which helps you track down lots of bugs by narrowing down what the possible problems could be. They are typically only evaluated in a special "debug" build of your application, so they won't slow down the final release version.

Let's say you wrote a function that took a pointer as an argument. There's a good chance that your code will assume that the pointer is non-NULL, so why not explicitly check that with an assertion? Here's how:

#include <assert.h>

void function(int* pointer_arg)
{
    assert(pointer_arg != NULL);

    ...
}

An important thing to note is that the expressions you assert must never have side effects, since they won't be present in the release build. So never do something like this:

assert(a++ == 5);

Some people also like to add little messages into their assertions to help give them meaning. Since a string always evaulates to true, you could write this:

assert((a == 5) && "a has the wrong value!!");
like image 133
andygeers Avatar answered Sep 20 '22 23:09

andygeers


Assertion are boolean expressions which should typically always be true.

They are used to ensure what you expected is also what happens.

void some_function(int age)
{
     assert(age > 0);
}

You wrote the function to deal with ages, you also 'know' for sure you're always passing sensible arguments, then you use an assert. It's like saying "I know this can never go wrong, but if it does, I want to know", because, well, everyone makes mistakes.

So it's not to check for sensible user input, if there are scenario's where something can go wrong, don't use an assert. Do real checks and deal with the errors.

Asserts are typically only for debug builds, so don't put code with side effects in asserts.

like image 38
Pieter Avatar answered Sep 20 '22 23:09

Pieter