I'm not particularly disagreeing with the point you are making - but would it really be asking to much for people to provide explanations of why one form is preferred over another?
The else block in this case is executed when "NULL != fname" is false. That's an unnecessary double negative and takes a nonzero amount of mental effort to unscramble. The example I gave reverses this. This would also be acceptable:
if(fname) {
g = fopen(fname, "w+");
} else {
g = tmpfile();
}
(I also don't regard either statement as an "exceptional condition", they both seem equally likely to me.)Rather than writing:
if (argc == 3) {
if (fname) {
/* Do stuff */
} else {
printf ("File name cannot be null");
}
} else {
printf ("Wrong number of arguments");
}
I would write: if (argc != 3) {
printf ("Wrong number of arguments");
} else if (!fname) {
printf ("File name cannot be null");
} else {
/* Do stuff */
} if (argc != 3) {
printf ("Wrong number of arguments");
// throw an exception or return NULL or something
}
if (!fname) {
printf ("File name cannot be null");
// throw an exception or return NULL or something
}
/* Do stuff */
The important thing, to my mind, is the absence of an else block.