One thing that I like in GLib is this macro: “g_return_if_fail” and “g_return_val_if_fail”.
But if you don’t use GLib or you have your own trace system, you can create your own macro, something like this:
#define return_if_fail(expr) if (!(expr)) {trace("[%s] - expr '%s' failed", __FUNCTION__, #expr); return;};
#define return_val_if_fail(expr, val) if (!(expr)) {trace("[%s] - expr '%s' failed", __FUNCTION__, #expr); return val;};
And with this macro, you can change your way to check the intergrity of your variables:
Common way:
if(var == NULL) {
printf("var == NULL\n");
return 1;
}
You can use:
return_val_if_fail(var != NULL, 1);
PS.: if you don’t have your own trace function, you can just change the “trace” for “printf” or “fprintf” function.
This is an easy, fast and nice way to trace the problems.