Should I Use Asserts?

Should I Use Asserts?

Assertion should be used for anticipating error in the way a programmer is using an API/function/class/whatever. These bugs need to be fixed quickly at debug time. For everything else, throw an exception. assert() macro is used to test the conditions or assumptions that should not occur in a program.

What are asserts used for?

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 state after it finishes running. Unlike normal exception/error handling, assertions are generally disabled at run-time. Arguments to private methods.

Is assert good or bad?

assert() is just as useful in C++ as in C. Assertions are not for error handling, they're for aborting the program immediately.

Alexander Ross
Author

Alexander Ross

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.