views:

170

answers:

8

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?

+2  A: 

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.

Michael Dorgan
Using assertions as warnings is an error and indicates a misunderstanding of what the word "assert" means.
dash-tom-bang
We have some special case debug code that throws a message up similiar to an assert, but allows execution to continue. It's called assertWarn(). More serious than a printf/log, but not worthy of a stop. I'm curious what others would call this.
Michael Dorgan
+3  A: 

Hi there.

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.

Cheers. Jas.

Jason Evans
+6  A: 

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

Longpoke
Why else? I don't know, that's why I asked. I regularly file bugs against developers for assert pops that they claim are "by design". Just wanted to make sure they are wrong / I'm on the same page as the s/w world :)
RichAmberale
Well if it's expected to fail, it doesn't really meet the requirement for an assertion. When you say, "I assert that this is true," you're not saying it's sometimes or usually true, you're saying that it's *always* true.
dash-tom-bang
It was a rhetorical question. An assertation is an expectation. `assert(x == 5)` means, "I think x will be 5 here". However, it is somewhat abused in low level code, for example, some people say you _must_ only use the API a certain way or _undefined behaviour_ will result, and then sometimes they stick in assertions to enforce checking against invalid input anyways, which just issues a warning or exits the program. This is bordering on stupidity but anything other than this defeats the purpose of an assertion.
Longpoke
+5  A: 

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.

Nick D
A: 

If you are trying to be logically inclusive about all the possibilities, remember that electronic circuitry is known to be affected by radiation from space. If the right photon/particle hits in just the right place at just the right time, it can cause an otherwise logically impossible state transition.

The probability is vanishingly small but still non-zero.

Kelly French
+1  A: 

If you are using assertions you're following Bertrand Meyer's Design by Contract philosophy. It's a programming error - the contract (assertion) you have specified is not being followed by the client (caller).

arcticpenguin
Of course, the contract may not be the correct one. I've done that before. However, either the contract or the client is buggy.
David Thornley
Actually no. The assertion is stated by the contract. If the client doesn't conform, then that is the issue.
arcticpenguin
A: 

I can think of one case that wouldn't really class as a bug:

An assert placed to check for something external that normally should be there. You're hunting something nutty that occurs on one machine and you want to know if a certain factor is responsible.

A real world example (although from before the era of asserts): If a certain directory was hidden on a certain machine the program would barf. I never found any piece of code that should have cared if the directory was hidden. I had only very limited access to the offending machine (it had a bunch of accounting stuff on it) so I couldn't hunt it properly on the machine and I couldn't reproduce it elsewhere. Something that was done with that machine (the culprit was never identified) occasionally turned that directory hidden.

I finally resorted to putting a test in the startup to see if the directory was hidden and stopping with an error if it was.

Loren Pechtel
A: 

No. An assertion failure means something happened that the original programmer did not intend or expect to occur.

This can indicate:

  • A bug in your code (you are simply calling the method incorrectly)

  • A bug in the Assertion (the original programmer has been too zealous and is complaining about you doing something that is quite reasonable and the method will actually handle perfectly well.

  • A bug in the called code (a design flaw). That is, the called code provides a contract that does not allow you to do what you need to do. The assertion warns you that you can't do things that way, but the solution is to extend the called method to handle your input.

  • A known but unimplemented feature. Imagine I implement a method that could process positive and negative integers, but I only need it (for now) to handle positive ones. I know that the "perfect" implementation would handle both, but until I actually need it to handle negatives, it is a waste of effort to implement support (and it would add code bloat and possibly slow down my application). So I have considered the case but I decide not to implement it until the need is proven. I therefore add an assert to mark this unimplemented code. When I later trigger the assert by passing a negative value in, I know that the additional functionality is now needed, so I must augment the implementation. Deferring writing the code until it is actually required thus saves me a lot of time (in most cases I never imeplement the additiona feature), but the assert makes sure that I don't get any bugs when I try to use the unimplemented feature.

Jason Williams