Jump to content


Photo

Why So Many Asserts


  • Please log in to reply
2 replies to this topic

#1 Judeau

Judeau

    Sergeant

  • Forum Members
  • PipPipPip
  • 36 posts

Posted 25 December 2005 - 06:20 AM

why is the assert used so often in the code?, wouldn't using if statements that change the value that is being checked be more reliable?
Posted Image

#2 Serge

Serge

    Project Leader: UFO 2000

  • Xenocide Programming Department
  • 785 posts

Posted 25 December 2005 - 08:40 AM

why is the assert used so often in the code?, wouldn't using if statements that change the value that is being checked be more reliable?

Asserts are used as guard checks for the conditions that should be never false by design. If assert gets triggered, there is something wrong happening and it needs our close attention and special investigation. Using 'if' statements everywhere would just hide bugs and lead to unpredicted behaviour. Not to mention that writing error handler for the error that is 'theoretically impossible' to happen is a waste of time and addition of extra lines of code :)

Also when running on very slow hardware, compiling with -DNDEBUG would remove all the assert checks from the code completely and improve performance. But in beta builds more error checks are obviously more important than a very little performance gain.

But some asserts are inserted as placeholders for proper error handling when writing new code and when we don't know for sure in which conditions this error could arise and how we should handle it.
ufo2000 development team
http://ufo2000.sourceforge.net

#3 Judeau

Judeau

    Sergeant

  • Forum Members
  • PipPipPip
  • 36 posts

Posted 25 December 2005 - 12:07 PM

i see, so basically you use them as error checking , to backtrace from that assert and see what could have gone wrong
i assume stable builds don't have asserts for reducing of cpu
Posted Image