Reputation: 229421
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.
Upvotes: 2
Views: 1076
Reputation: 616
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.
Upvotes: 0
Reputation: 8190
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;
Upvotes: 1
Reputation: 9565
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.
Upvotes: 0
Reputation: 14057
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.
Upvotes: 0
Reputation: 18782
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.
Upvotes: 2
Reputation: 759
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).
Upvotes: 0
Reputation: 7026
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
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.
Upvotes: 0