Most of the statements in the project I work with is of the form
if(!someObject->someFunction(arg1,arg2))
{
cerr << "Something bad happened. Args are: " << arg1.print() << " " << arg2 << endl;
return false;
}
This makes my code not readable as error logging triples the actual code. Hence I wrote the following macro,
#define ReturnFalse(error) \
({ \
cerr << error << "@File: " << __FILE__ << "Line: " << __LINE__ << endl; \
return false; \
false; \
}) \
Now I can use it as,
someObject->someFunction(arg1,arg2) || ReturnFalse("Something bad happened.Args are:" << arg1.print() << " " << arg2 );
This works but I am afraid if I am following bad practice.
- Is the code illegal/undefined?
- In performance wise, is it any worse than if?
- My vim does not recognize the formatting and highlight as if there is some syntax error. Is there anyway to fix this?
- Is there a better way to log a message and return with a neat syntax?
- It is not straight forward that the macro could return from the function. So I named it
ReturnFalse
. Can anyone suggest a better name?
Edit:
Thanks for all replies.Is there a standard way to do it using comma operators? I am not able to get it right.