28 ms·
For what it's worth I couldn't disagree more. I use the guard pattern very often (as described here http://video.ias.edu/webfm_send/1207 http://video.ias.edu/we
by VBprogrammer 15y ago
For what it's worth I couldn't disagree more. I use the guard pattern very often (as described here http://video.ias.edu/webfm_send/1207 http://video.ias.edu/webfm_send/1207 [Video]).
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 */
}
- qntm 15y agoThat has merit. Personally I usually find myself doing something more like this: 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.
- VBprogrammer 15y agoYeah, that is a pattern I occasionally use but I prefer what I have written above as the control flow is more explicit. A missing return (or even one which is quite deliberately omitted) is much harder to spot.