Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Benefits of Assertive Programming

What is the point of putting asserts into our code ? What are the benefits of assertive programming ?

private void WriteMessage(string message)
{
    Debug.Assert(message != null, "message is null");

    File.WriteAllText(FILE_PATH, message);
}

For example we can check the message variable and throw an exception here. Why do I use assert here ? Or is this a wrong example to see benefits of asserts ?

like image 421
Canavar Avatar asked Apr 24 '09 21:04

Canavar


People also ask

What is assertive programming?

Assertive programming follows the principles of fail fast and fail visibly. It is implemented by issuing an informative error message if the function arguments fail to satisfy specific criteria. This is particularly important in R because it is a dynamically typed language.

What is the purpose of assertions?

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.

What is a coding assertion?

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.


4 Answers

They also support the philosophy of fail fast, explained in this article by Jim Shore.

like image 182
Shane Fulmer Avatar answered Sep 19 '22 09:09

Shane Fulmer


Where some people would write:

/*
 * This can never happen
 */

Its much more practical to write:

assert(i != -1);

I like using asserts because they are easily turned off with a simple compile time constant, or made to do something else like prepare an error report. I don't usually leave assertions turned on (at least, not in the usual way) when releasing something.

Using them has saved me from making very stupid mistakes on other people's computers .. those brave souls who enjoy testing my alpha code. Using them plus tools like valgrind helps to guarantee that I catch something awful before committing it.

like image 24
Tim Post Avatar answered Sep 20 '22 09:09

Tim Post


An important distinction to consider is what sorts of errors you would like to catch with assertions. I often use assertions to catch programming errors (i.e., calling a method with a null parameter) and a different mechanism to handle validation errors (e.g., passing in a social security number of the wrong length). For the programming error caught with the assertion, I want to fail fast. For the validation error, I want to respond less drastically because it may be normal for there to be errors in the data (e.g. a user doing data entry of some sort). In those cases the proper handling might be to report the error back to the user and continue functioning.

like image 26
Rob Scott Avatar answered Sep 19 '22 09:09

Rob Scott


If there is a specified pre-condition for the method to take a non-null message parameter, then you want the program to FAIL as soon as the pre-condition doesn't hold and the source of the bug must then be fixed.

I believe assertions are more important when developing safety-critical software. And certainly you would rather use assertions when the software has been formally specified.

like image 39
Peter Perháč Avatar answered Sep 20 '22 09:09

Peter Perháč