Quick question about returning from a nested statement

341 views Asked by At

If I have something like a loop or a set of if/else statements, and I want to return a value from within the nest (see below), is the best way of doing this to assign the value to a field or property and return that?

See below:

bool b;

public bool ifelse(int i)
{
if(i == 5)
{
b = true;
}

else
{
b = false;
}
return b;
}
6

There are 6 answers

4
Doctor Jones On BEST ANSWER

Yes, that is good style.

The alternative (which would be bad) would be to do this:


public bool ifelse(int i) 
{ 
    if(i == 5) 
    { 
        return true; 
    }
    else 
    { 
        return false; 
    }
}

The reason multiple return points are regarded as bad style is that especially for larger methods it can be difficult to keep track of the program flow within a method because it could exit at any point. This can be a nightmare to debug. If you have a return variable that you assign to however, you can watch that variable and know exactly when it will be returned (from a single place).

This isn't always the case, as with every stylistic point in programming there are good sides to it and bad sides.

4
gcores On

what about

return i == 5;
0
DaEagle On

There are multiple views on this. I think most people (including me) tend to prefer to return as soon as you have an answer and there is no more work to do. Some people will argue that you should only ever return at the last statement of a method. However, it can actually make things more complicated in some situations.

Going by what I've suggested, your example would be shorter and simpler:

public bool ifelse(int i)
{
if(i == 5)
{
return true
}
return false
}
0
Chris Shaffer On

I would say if you should generally only return from a method in two places - near the beginning (as in guard conditions) and near the end; If the method has any length to it, you should use a temporary variable as you mentioned, otherwise people reading the code may have a harder time following it.

0
foxy On

If b is only used to calculate the return value for your method then you should make it local variable (defined within the method).

public bool ifelse(int i)
{
  bool b;
  /*
  Some code to calculate b
  */
  return b;
}

As others have suggested, If your method is simple I would avoid using a temporary variable altogether and return the result as soon as it is known. A general rule would be to use whichever method makes the code easiest to read.

0
BCS On

As pointed out, having more than one return statement has the downside of finding them gets hard. OTOH in some cases the added logic needed to escape to that return statement is worse that the problem the style is solving.

The major problem I'm aware of re multiple returns is that you can quickly forget to do some cleanup processing or the like at a new return point. IMHO this is just as much a problem with the single return form because the escape path has to remember to include that code and no other code. One solution to this, available in some languages like c#, is the finally block or it's neater form the scope statement as demonstrated here. (OK I'll get of my soap box now)