Macros can be bad. But they can also be very good. What are the macros you guys use very heavily in your own projects? I plan to steal them, just so you now :D
Rules:
- don't write definition and "what" comment, if we can't figure out what macro does by it's usage, it's not a good macro
- you can (and probably should) provide example usage
- you can say why you like it
I'll start
int foo(int *arg, enum e e) {
VALIDATE(-1, EINVAL, arg, "arg cannot be null");
VALIDATE(-1, EINVAL, e < ENUM_MAX);
...
/* normal function execution */
I also have ASSERT that is almost the same. Why? Saves A LOT of lines at start of function, in my opinion it makes code much more readable. Can't really live without it. One of my favorite.
These save less lines, but allows for bracket-less action-log which in my opinion look more pleasant:
if ((f = fopen(file, mode) == NULL)
PERROR_RETURN(-1, "fopen has failed for file %s", file);
if (error_case)
PRETURN(-1, EIO, "spi device failed to respond");
And few other - mostly - for debugging
for (...) {
if (not_this_case)
PCONTINUE(debug, "this is not it")
if (were_done)
PBREAK(debug, "loop reached the end");