Our Properties: Gamasutra GameCareerGuide IndieGames Indie Royale GDC IGF Game Developer Magazine GAO
My Message close
Latest News
spacer View All spacer
 
February 10, 2012
 
Road to the IGF: Lucky Frame's Pugs Luv Beats
 
Analyst questions validity of unusual January NPD results [4]
 
Strong Tales of Xillia sales help Namco Bandai to Q3 profits
spacer
Latest Features
spacer View All spacer
 
February 10, 2012
 
arrow Principles of an Indie Game Bottom Feeder [20]
 
arrow Postmortem: CyberConnect 2's Solatorobo: Red the Hunter [1]
 
arrow Jerked Around by the Magic Circle - Clearing the Air Ten Years Later [40]
spacer
Latest Jobs
spacer View All     Post a Job     RSS spacer
 
February 10, 2012
 
Beachhead / Activision
Senior Operations Engineer
 
Sony Computer Entertainment America LLC
Manager Quality Assurance
 
Beachhead / Activision
Senior Front End Web Engineer
 
Beachhead / Activision
Senior Back End Web Engineer
 
Beachhead / Activision
User Experience Lead
 
Sony Computer Entertainment America LLC
Senior Software Engineer
spacer
Blogs

  Code Assertively
by Neil Gower on 08/01/09 07:00:00 pm   Expert Blogs
4 comments Share on Twitter Share on Facebook RSS
 
 
  Posted 08/01/09 07:00:00 pm
 

Good old assert. Such a well meaning tool that can cause such a ruckus. Assert gets some bad press because it can be so jarring when it works. The documented behaviour for assert in C++ is:

An assert() evaluates its argument and calls abort() if the result is zero (false).

The C++ Programming Language, Special Edition pg 750

In a typical Windows game, the abort part means the DUNH! system sound and an angry red dialogue box telling you the fun is over. If you're not a programmer, you might wonder why anyone would intentionally put this kind of thing in their code. If you are a programmer, you've probably noticed how similar an assert feels to an unhandled exception (i.e. a real crash). This causes everyone to lump asserts into the same mental category as "somebody broke the code".

To be fair, when an assertion fails it does means something bad has happened. Something that the code isn't built to handle. Something that if leRetry or ignore?ft unchecked will probably lead to a crash (though not always right away), maybe trashing some files in the process, wiping highscores from the server, or some other unpredictable peril. 

When something goes wrong in a program, it's like flying a helicopter as a part comes loose. Assert would be like freezing time right when that bolt falls off to figure out what went wrong. Not using assert is analogous to crashing and burning and then digging through the wreckage to figure out what happened. 

You can attach the debugger when your game asserts and take a look at what's going on - inspect variables, look at the call stack, and so on. Compared to tracing back from an often unrelated symptom, this kind of debugging is easy.

The assert macro in C++ does nothing in non-debug builds (i.e. when NDEBUG is defined). While this is good in principle, you can never know for certain that there isn't some obscure scenario that could trigger an assert, so it can be useful to have some around even in optimized builds. For this purpose, you probably want to write your own assert function/template/macro that ties into your error handling system and does something a little more graceful than abort(). Something like submitting a bug report would be ideal.

The catch: Assert is not a substitute for error handling. Asserts in production code should only be used to detect things that are impossible, or at least would be impossible if the code was written correctly. They're a tool for detecting coding errors.

Things that aren't coding errors are things like missing files, failed network connections, out of memory errors, to name a few. It's not the program's fault when these things happen. They are runtime errors that you have to detect and deal with yourself.

Assert is a useful tool for building solid, reliable code. The more you use it, the closer to the real problem you'll find yourself when something goes wrong. Don't be afraid to use assert - and if the DUNH! sound bothers you, you can always try changing your "Critical Stop" system to something less jarring.

 
 
Comments

Stephen Northcott
profile image
Nice article. My development code is peppered with my own Assert Macros.

I would go one step further and recommend that people build a Kernel which controls their run loop, and allows runtime Logging and Profiling. The one that I have built up over the years is pretty much 100% portable and I use it on OS X, Windows and also on the iPhone.

I am a fan of Singletons for that purpose. I know some people are not. If this was GameDev I would already have been lynched by now.. but still.. it works for me. :)

Jimmy Sieben
profile image
Assert is certainly a useful tool, but I have found it is a bit much to use in a game's editing tools.

The issue here is that an assert can crash the editor and leave the user without an opportunity to save work.

For truly catastrophic conditions (out of memory/handles/etc...) assert may be your only hope. But for the average "this condition shouldn't be possible" it might not be a good strategy.

Alexander Bruce
profile image
Assert is useful, though I've definitely seen it used in cases that should have been an immediate sign to fix the code as fast as possible.

*I'm looking at you asserts that fire off every frame in someone elses code*!

Edgar Batnya
profile image
You can always build your code around stack unwind, providing automatic means of exiting impossible scenarios.


none
 
Comment:
 




 
UBM Techweb
Game Network
Game Developers Conference | GDC Europe | GDC Online | GDC China | Gamasutra | Game Developer Magazine | Game Advertising Online
Game Career Guide | Independent Games Festival | Indie Royale | IndieGames

Other UBM TechWeb Networks
Business Technology | Business Technology Events | Telecommunications & Communications Providers

Privacy Policy | Terms of Service | Contact Us | Copyright © UBM TechWeb, All Rights Reserved.