views:

93

answers:

8

Which coding style do you prefer, and why?

if case1:
    return res1
if case2:
    return res2
return res3

or:

if case1:
    return res1
elif case2:
    return res2
else:
    return res3

or:

res = None
if case1:
    res = res1
elif case2:
    res = res2
else:
    res = res3
return res

Reason: I have code that looks like this, and I'm wondering what's the clearest way of expressing it. Personally I can't decide between the 1st and the 2nd, and I wouldn't consider the third.

I was about to tag this language-agnostic, but I realized that functional languages don't have this issue, as it defaults to case 2 =P.

A: 

The first and second aren't really equivalent.

The first doesn't entail the conditions are mutually exclusive.

If they are, use 2 or 3.

I prefer option 3, because, with the others you are in trouble if you want to perform some pre-return function or common postprocessing.

Instead you could use the 3rd option without the else:

res = res3   // Default
if case1:
   res=res1
elif case2:
   res=res2
postprocessing
return res

edit

yes you are right, it is mutually exclusive with the return. But I suppose I don't like it because the if structure itself doesn't capture that.

Sanjay Manohar
Actually, the first does make the conditions mutually exclusive. That's what the 'return' does. If case1: return res1 will tell the method that its done and to give back the first result. At least, it will in C#, and I'm pretty sure most of the other languages as well.
AllenG
yep it is, it's just not as obvious.
Claudiu
A: 

Typically I would go with the 2nd style. It conveys your intentions to another programmer in a very clear manner IMO. However I agree with Sanjay that the 3rd option is nice (almost required) for any post processing (thought that did not appear to be your intent).

JMcCarty
it wasn't, in this case. for post-processing i sometimes find it easier to turn the function into a helper function, and write a new func that calls the helper and does post processing on whatever result it returns. this is especially useful if there are 2 or 3 blocks of if/elif/elses that return, with more processing later (think error handling where an error doesn't mean you throw an exception, just a different result)
Claudiu
+1  A: 

If you have a switch (or case) statement available to you, and the cases are evaluating the same variable use that:

In C#

Switch(Condition)
{
  case 1:
    return res1;
  case 2:
    return res2;
  default:
    return res3;    }

or, if you have your variable set up first, change those return statements to variable assignments, and don't forget to add your break;

If your cases are not evaluating the same variable, then I prefer 2 (readability)

if(variable1 = x) return result1;
else if(variable2 = y) return result2;
else return result3;
AllenG
+2  A: 

If each of those is the entire function, then it probably does not matter. For more complicated functions, I would prefer the third, as returns from the middle of functions can be easy to miss when reading the code a few months later.

On a similar note, I would avoid the first because if someone decided later that they wanted to move to something like the third answer for some post processing they might miss that the conditions are not mutually exclusive. This can lead to happy fun debug time.

deinst
A: 

I use case #1 quite regularly. A good chunk of the routines I write must validate user input. The further I get into the routine, the more correct I know things have to be, and the fewer items/conditions I need to track. If I can determine that the input is invalid for any reason, I bail.

People's habits and styles vary, but I see the 'else' in the 'else if' as superfluous in the examples listed in the question. If present, I expect there to be some form of post-processing, and must then track states/variables of preceding if-statements. Otherwise I prefer to use if-then-bail when practical.

Sparky
A: 

To determine between #2 and #3, check out this question: http://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement

Charles Boyung
A: 

I personally prefer the first one. But I guess I'm bias because I'm all about fewer lines of code.

As far as readability goes, I would have to say case 2 is the most clear option. I don't like case 3 because it's introducing a new variable when it is not really necessary, and thus seems redundant.

townsean
A: 

The third one obviously makes it easier to change the function should the return code ever change uniformly over all cases (an example would be switching units or something like that). Apart from that, IMHO number one suites trivial functions best - it's clean and small and absolutely obvious. And the additional future refactoring advantage that the third version might have is something which every good editor will compensate for easily.

I would say: pick anyone and move on.

PS: It doesn't even trigger some sort of compiler magic, at least not in C and on my machine (gcc 4.4.3, Linux). All 3 variations compile to completely equal code.

RWS