Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should assert() be used?

Tags:

c++

assert

In developing a large C++ programming project with many developers, we have run into issues with inappropriate use of assert() in the code which results in poor quality where the assertion does indeed occur and the product crashes.

The question is what are good principles to apply to use assert() appropriately? When is it proper to use an assert() and when is it not? Is there a list of criteria that each assertion should pass in order to be legitimate? How can we encourage proper use of assert()?

As a first crack at this, I would say that assert() should only be used to document a condition that is believed to be impossible to reach and which should be identified as an assert() failure at run time where it ever to arise because programming assumptions are being violated.

Can folks do better than this? What is your experience with assert()?

like image 645
WilliamKF Avatar asked Sep 13 '10 16:09

WilliamKF


People also ask

When should you 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. However, you may get a 'non-reachable' code error.

When should I use assert in C?

You should only use assert to check for situations that "can't happen", e.g. that violate the invariants or postconditions of an algorithm, but probably not for input validation (certainly not in libraries). When detecting invalid input from clients, be friendly and return an error code.

Is it good practice to use assert?

Assertions will help only during internal testing to catch failure of assumptions. You should not assume the behavior of external agencies, so you should not assert on events from the network or user. Also it is a good practice to write handling code for production builds in case an assertion fails.

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.


3 Answers

Use Exceptions for error condition which come from the outside (outside the method or outside the program) like parameter checking and missing/defective external ressources like files or connections or user input.

Use Assertions to indicate an internal defects like programming errors, conditions that shouldn't occur, e.g. class/method invariants and invalid program state.

like image 158
codymanix Avatar answered Oct 15 '22 14:10

codymanix


You should use assert to check all conditions that should never happen:

  • Preconditions on input parameters
  • Results of intermediate calculations
  • Postconditions on object state

But you should include those asserts only in debug builds or when explicitly activated for release (not in builds released to the customers).

like image 20
ggarber Avatar answered Oct 15 '22 14:10

ggarber


I use asserts to check for any unwanted program state:

  • Function preconditions
  • Sometimes I insert them in a macro after every API call: glDrawArray(); checkOpenGLError();--checkOpenGLError() will call getGLError() if turned on
  • Data structure integrity: assert(something == null);
  • Sometimes GDB lies to me (iOS SDK 3.2). I use asserts to prove it.

Note that "unwanted program state" excludes errors that naturally occur at runtime, such as being unable to open a user-selected file due to permissions or HD failure. In these cases it is not wise to use assertions.

like image 1
MrAnonymous Avatar answered Oct 15 '22 14:10

MrAnonymous