Click here to Skip to main content
15,889,096 members
Articles / Programming Languages / C++
Tip/Trick

Verify

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
2 Jul 2013BSD2 min read 19.3K   130   5   8
A Practical Alternative to Assert in C++

Background

I confess that in the past, I have abused assert(). I used it for handling nearly all of my errors, and rarely used return values or anything else. When I started taking computer science classes, I was introduced to try, catch, and exceptions, and using them I was able to write more productive and useful code.

However, there is one thing that I believe assert is superior at: aesthetics. When writing code, I much prefer the brevity of the assert macro. It's simpler, takes up less space, and is overall much more pleasing to look at than if statements and throws.

Verify was my solution. A macro which has the brevity of assert, but uses the much more practical Exceptions.

Using the Code

Verify is a two parameter macro. The first parameter is the condition to test, the second is the Exception that will be thrown if the condition is false.

C++
#include "verify.h"

void func1(void* ptr)
{
    verify(ptr != NULL, -1);
}

Verify has three modes:

Standard

The simplest mode, if the condition is false, it throws the Exception. The End.

Debug

Debug mode carries over some features I found useful in the assert macro. If the condition is true, then nothing happens, but if it is false, it prints out the following debug information to the screen:

  • The condition passed to the macro.
  • The exception that was thrown.
  • File name
  • Function name
  • Line number

Then it throws the Exception.

No Verify

The equivalent of NDEBUG for assert. If NVERIFY is defined ahead of the verify header, then verify() has no effect.

Conclusion

And that's pretty much it. Short, sweet, and to the point. If you can think of any improvements or find any problems, please let me know in the comments.

I'd like to thank Charles Nicholson for his article Adventures in Assert, which was a great help in solving several bugs I had in my original code.

License

This article, along with any associated source code and files, is licensed under The BSD License


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionI am confused Pin
geoyar1-Jul-13 5:58
professionalgeoyar1-Jul-13 5:58 
GeneralYou don't seem to understand the concept behind assert and verify Pin
pasztorpisti28-Jun-13 8:58
pasztorpisti28-Jun-13 8:58 
Both assert and verify are already existing "thingies": http://www.gamedev.net/page/resources/_/technical/general-programming/assert-verify-and-trace-for-non-mfc-appl-r1846[^]
I guess you didn't know about the "original verify" known by a lot of programmers (mostly from mfc) so you named your assert alternative the same that is confusing. Your assert alternative has 2 serious glitches: assert must evaluate to nothing in release mode and mixing the concept of exception handling and assertions is not a good idea. exceptions are used mostly for error handling to alter control flow easily and return easily from deep function calls while assertions are only for the programmers to help checking (usually preconditions) and causing a __debugbreak() (simulated breakpoint) when the check fails.

The only practical assert alternative can be an assert macro that behaves like the original but is nicer than that. For example our assert macro brings up a nice window that has break/continue/ignore buttons and we have some other useful assert macro alternatives like assertm and assertcm that allow you to assert with a specified message and optionally without specifying any condition.

If you want to check a condition and then throw an exception in your program then you can define your own macro for that but naming it with the same name used by a well defined existing macro is still a bad idea.

Edit: Suggested features that could be an improvement in an assert alternative:
- Works consitently on multiple platforms (win32/64, macosx, ios, linux, android, ...)
- Multiple macros:
* assertm(message): assert that fires unconditionally with the specified string message like assertm("This should never happen. Smile | :) ")
* assertcm(condition, message)
* assert(condition)
- has a reliable "ignore" choice that never fires the same assert till the end of the current debugging session
- outputs some debug log (for example with OutputDebugString() on windows).
- works well in a multithreaded environment and prevents multiple threads from popping up many assert windows that can make debugging impossible in some scenarios.

modified 28-Jun-13 15:22pm.

GeneralRe: You don't seem to understand the concept behind assert and verify Pin
Jacob F. W.28-Jun-13 11:22
Jacob F. W.28-Jun-13 11:22 
GeneralRe: You don't seem to understand the concept behind assert and verify Pin
pasztorpisti28-Jun-13 12:26
pasztorpisti28-Jun-13 12:26 
GeneralRe: You don't seem to understand the concept behind assert and verify Pin
Jacob F. W.28-Jun-13 17:23
Jacob F. W.28-Jun-13 17:23 
GeneralRe: You don't seem to understand the concept behind assert and verify Pin
pasztorpisti29-Jun-13 7:02
pasztorpisti29-Jun-13 7:02 
QuestionNot enough... Pin
Mehdi Gholam27-Jun-13 20:18
Mehdi Gholam27-Jun-13 20:18 
AnswerRe: Not enough... Pin
Jacob F. W.27-Jun-13 20:21
Jacob F. W.27-Jun-13 20:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.