简体   繁体   中英

In C language, what is the best practice to check return value of a function for a branching statement?

I'm trying to have an embedded software development point of view, and I'd like to ask which one is better to go with, and what are the possible advantages and disadvantages?

bool funct(){
   bool retVal = 0;
   //do something
   return retVal;
}

//First Choice
if(funct()){
   //do something
}

//Second Choice
bool retVal = funct();
   if(retVal)
  {
    //do something
  }

Either is probably OK in this example, however the second has a slight advantage when debugging in that when stepping the code you will know whether the condition is true before the branch is taken and can coerce the variable to a different value if you want to test the alternate path, and being able to see the result of a call after the event is useful in any case during debugging.

In more complex expressions the approach may be more important, for example in:

if( x() || y() ) ...

if x() returns true, then y() will not be evaluated, which may or may not be desirable if y() has side effects, so the semantics of that are not the same as:

bool xx = x() ;
bool yy = y() ;
if( xx || yy ) ...

Using explicit assignment allows the required semantics to be clearly expressed.

//First Choise
if(funct()){
   //do something
}

This is totally fine as you check the return value of function to take the decision and your function returns either 0 or 1.

Also there is a advantage here over the second choice as you are saving space of one variable retVal just to hold the return value and perform the check.

If there is a need to use the return value not only just for the check in if condition and somewhere else in the program then I would suggest storing the return value (choice 2)

Both methods will work fine. If you define better as code that will execute (very slightly) faster and take up (very slightly) less room when it is compiled, then alternative 1) is better. Alternative 1) will read the value of the function into a register and branch on the value in two commands and use no memory. Alternative 2) will read the value of the function into register, write the value to memory, read the value from memory into a register and branch on the value - for a total of four commands and four bytes of storage (assumes a 32 bit processor).

The first choice (note the spelling) is better, but for reasons entirely unrelated to what you might think.

The reason is that it is one line of code shorter, and therefore you have one less line of code to have to worry about, one less line of code to have to read when trying to understand how it works, one less line of code to have to maintain in the future.

Performance considerations are completely pointless under any real-life scenario, and as a matter of fact I would be willing to guess that any halfway decent compiler will produce the exact same machine code for both of these choices.

If you have questions of such a basic nature, I would strongly advice you to quit trying to "have an embedded software development point of view". Embedded is hard; try for non-embedded which is a lot easier. Once you master non-embedded, then you can try embedded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM