0

If this has been answered elsewhere, I'd be happy just to follow a link.

Is it better programming practice to test the negative case first to fail quickly and keep the body of the function together OR to test the positive case first and have the negative case dangling at the end? Does it make a difference if the if-else statements are heavily nested? Example:

int foo(int x){
    int status = 0; //initialize to success
    if(x < 0){
       std::cout << "Bad parameter\n";
       status = -1;
    }else{
       /* execute the rest of the function */
    }
    return status;
}

OR

int foo(int x){
    int status = 0;
    if(x >= 0){
       /* execute body of the function */
    }else{
        std::cout << "Bad parameter\n";
        status = -1;
    }
    return status;
}
gorlux
  • 129
  • 5
  • If you can `return` from the first one, like `return -1`, then it can be first as a *guard condition*. – tadman May 23 '20 at 04:16
  • Thanks for the comment. I've seen this construct, but wondered if it still qualified as well-structured code since there is more than one return. – gorlux May 23 '20 at 04:22
  • 2
    I generally steer towards the one that results in less overall indentation. The guard condition allows the rest of the function to remain at the same level as the function instead of all jammed into an `else`. Guard conditions area also easier to identify and understand if near the top. It's more self-explanatory as to what limits are in place. Sneaky guard conditions near the bottom aren't. – tadman May 23 '20 at 04:31
  • 1
    Why would multiple returns not qualify something as "well-structured code"? – user975989 May 23 '20 at 04:36
  • 1
    I got that impression about well-structured code somewhere, but it seems to be the subject of some debate: https://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement – gorlux May 23 '20 at 04:42
  • That's far from established doctrine, though I have seen some "house rules" at organizations that mandate it. These sorts of rules can be pretty absurd, like "no function over 30 lines long" or "no more than three levels of indentation" so take them all with a grain of salt. – tadman May 23 '20 at 04:46
  • 1
    Thanks for the comments. This has given me a much better perspective on the options and preferred practices. – gorlux May 23 '20 at 04:52
  • @user975989 -- some folks like the formality of having a single return. The claim being that you don't have to look for embedded `return` statements to figure out the control flow. I don't buy it; having a bunch of conditions and ending up with a bunch of `}`s to finish them off means having to look through all the structure to figure out where you are; I'd much rather have early `return`s. – Pete Becker May 23 '20 at 12:45

3 Answers3

4

There is no difference in efficiency, for all practical purposes. Because, in any case, you have to make exactly one comparison.

Some would say the first one is better because, generally the code for positive(non error) is larger and the first method highlights the corner case making it easy to read.

1

No difference in your case. Compiler is smart enough to generate machine code logic possibly reverting your comparison logic flow. Try pasting your code in https://godbolt.org/ and viewing asm instructions.

dgrandm
  • 375
  • 3
  • 12
0

It doesnt make much difference.I would go with the positive condition first before the negative condition.

Krish
  • 3
  • 2