logoalt Hacker News

throwaway2037 • today at 8:45 PM • 0 replies • view on HN

Unless you are doing trivial boolean comparisons, I always prefer a custom assert function that will also print a useful error message. Just telling me "Assertion failed: index < container.size()" isn't very helpful. What were the values compared?

Instead, my custom asset function (in C) looks like this:

    > void AssertWF(bool assertResult, char *format, ...)
Example:

    > AssertF(index < container.size(), "index:%d < container.size():%d", index, container.size());
If the first parameter is false, a detailed error message is printed:

    > fputs("Assert failed: ", stderr);
    > vfwprintf(stderr, format, ...);
    > abort();