|
|
|
|
|
by ChuckMcM
4571 days ago
|
|
Interestingly I've been going back and forth on this lately. I have been playing around with SD cards on the STM32F4 and a typical SD Card transaction consists of 3 to 10 commands which, if any one fails, the transaction fails. I'm currently using negative conditionals of the form "Not Error" (the Error test is an affirmative, and so that seems ok to me) A typical sequence is like the one to set the bus width. err = sdio_select(rca);
if (! err) {
err = sdio_command(55, rca << 16);
if (! err) {
err = sdio_command(6, 2);
}
}
sdio_deslect();
return err;
I had originally done it the other way err = sdio_select(rca);
if (err) {
return err;
}
err = sdio_command(55, rca << 16);
if (err) {
sdio_deselect();
return err;
}
err = sdio_command(6, 2);
sdio_deselect();
return err;
I find the first form more readable. It also generates
fewer branches in the generated code. The sample code I first looked at was doing Goto's to the exit code (deselect/return error) which was unacceptable :-).In the original article the confusion arose around negative test cases and then testing for them negatively (double negatives) which I think are always bad from a readability point of view. |
|
The sample code I first looked at was doing Goto's to the exit code (deselect/return error) which was unacceptable :-).
What's so bad about a little goto between friends?