Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any benefit of using assert instead of using a simple "if" ?

Given this code :

#include <stdio.h>
#include <assert.h>

void print_number(int* somePtr) {
  assert (somePtr!=NULL);
  printf ("%d\n",*somePtr);
}

int main ()
{
  int a=1234;
  int * b = NULL;
  int * c = NULL;

  b=&a;

  print_number (c);
  print_number (b);

  return 0;
}

I can do this instead :

#include <stdio.h>
#include <assert.h>

void print_number(int* somePtr) {
  if (somePtr != NULL)
       printf ("%d\n",*somePtr);
  // else do something 
}

int main ()
{
  int a=1234;
  int * b = NULL;
  int * c = NULL;

  b=&a;

  print_number (c);
  print_number (b);

  return 0;
}

So , what am I gaining by using assert ?

Regards

like image 464
JAN Avatar asked Jul 13 '12 10:07

JAN


People also ask

Is it good practice to use assert?

Don't use asserts for normal error handling Very similar to try and catch , asserts littered all over the code distract the programmer that is reading the code from the business logic. Validating user input — A good program always validates user input, but this should never be done with assertions.

When should I use assert?

You can use an assert to check if your logical assumption is correct. You can also use assert statements to check if the control flow is correct or not. For example, if you have a function that returns a value, you may want to put an assert statement.

Is it good to use assert Python?

Assert should only be used for testing and debugging — not in production environments. Because assert statements only run when the __debug__ variable is set to True , a Python interpreter can disable them.

Why do we need to assert?

Assertions are mainly used to check logically impossible situations. For example, they can be used to check the state a code expects before it starts running or the state after it finishes running. Unlike normal exception/error handling, assertions are generally disabled at run-time.


2 Answers

assert is to document your assumptions in the code. if statement is to handle different logical scenarios.

Now in your specific case, think from the point of view of the developer of the print_number() function.

For example when you write

void print_number(int* somePtr) {
  assert (somePtr!=NULL);
  printf ("%d\n",*somePtr);
}

you mean to say that,

In my print_number function I assume that always the pointer coming is not null. I would be very very surprised if this is null. I don't care to handle this scenario at all in my code.

But, if you write

void print_number(int* somePtr) {
  if (somePtr != NULL)
       printf ("%d\n",*somePtr);
  // else do something 
}

You seem to say that, in my print_number function, I expect people to pass a null pointer. And I know how to handle this situation and I do handle this with an else condition.

So, sometimes you will know how to handle certain situations and you want to do that. Then, use if. Sometimes, you assume that something will not happen and you don't care to handle it. You just express your surprise and stop your program execution there with assert.

like image 57
PermanentGuest Avatar answered Oct 07 '22 19:10

PermanentGuest


Lots of reasons:

  1. Asserts are usually removed for release builds.
  2. Asserts will report failure information to the client. if() does nothing by itself.
  3. Because asserts are usually macros, you can also get code information about the failing assertion.
  4. Assert is more semantically clear than if().
like image 45
tenfour Avatar answered Oct 07 '22 18:10

tenfour