Would anyone here recommend using debug statements such as the following in production quality code?
I think these are personally one of the easiest to include or exclude, but they make the code hard to read so alternatives would be better
#ifdef DEBUG
perror("open");
exit(1);
#endif
// Otherwise here we just skip the debug error or try to recover?
I've seen people use "debug print" macro's too, similar to these
#define TRACE(args) do { fprintf args } while (0)
// And use them like this
TRACE((stderr, "Failed to open file at path /.app/config"));
// And when debugging is disabled they change the macro to
#define TRACE(args) do {} while(0)
But the second technique seems rather messy to me, it's more flexible with variable argument lists but i prefer to use C89 so this is not a possibility.
We can also use asserts of course, but this is rather less descriptive than our own defined error messages. However i like them very much since they're super easy to enable or disable without a mess, and on top of that it prints the line / file where the error occurred.
int fd = open("testfile.c", O_RDONLY | O_NONBLOCK);
assert(fd != -1);
What is the accepted method to include debug statements in programs? Or should we just wing the whole idea and scrap debug statements in general.
I really would like to use debug statements in my program so that errors are more clear, but is it even worth it?