Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If as assert fails, is there a bug?

I've always followed the logic: if assert fails, then there is a bug. Root cause could either be:

  • Assert itself is invalid (bug)
  • There is a programming error (bug)
  • (no other options)

I.E. Are there any other conclusions one could come to? Are there cases where an assert would fail and there is no bug?

like image 403
noctonura Avatar asked May 20 '10 21:05

noctonura


4 Answers

If assert fails there is a bug in either the caller or callee. Why else would there be an assertion?


Yes, there is a bug in the code.

Code Complete

Assertions check for conditions that should never occur. [...]

If an assertion is fired for an anomalous condition, the corrective action is not merely to handle an error gracefully- the corrective action is to change the program's source code, recompile, and release a new version of the software.

A good way to think of assertions is as executable documentation - you can't rely on them to make the code work, but they can document assumptions more actively than program-language comments can.

like image 44
Nick Dandoulakis Avatar answered Nov 22 '22 21:11

Nick Dandoulakis


That's a good question.

My feeling is, if the assert fails due to your code, then it is a bug. The assertion is an expected behaviour/result of your code, so an assertion failure will be a failure of your code.

like image 34
Jason Evans Avatar answered Nov 22 '22 21:11

Jason Evans


Only if the assert was meant to show a warning condition - in which case a special class of assert should have been used.

So, any assert should show a bug as you suggest.

like image 29
Michael Dorgan Avatar answered Nov 22 '22 22:11

Michael Dorgan