mankand007
mankand007

Reputation: 992

else vs return to use in a function to prematurely stop processing

[Edit] changed return 0 to return. Side effects of beinga Python n00b. :)

I'm defining a function, where i'm doing some 20 lines of processing. Before processing, i need to check if a certain condition is met. If so, then I should bypass all processing. I have defined the function this way.

def test_funciton(self,inputs):
    if inputs == 0:
        <Display Message box>
        return
    <20 line logic here>

Note that the 20 line logic does not return any value, and i'm not using the 0 returned in the first 'if'.

I want to know if this is better than using the below type of code (in terms of performance, or readability, or for any other matter), because the above method looks good to me as it is one indentation less:

def test_function(self,inputs):
    if inputs == 0:
        <Display Message box>
    else:
        <20 line logic here>

Upvotes: 7

Views: 10665

Answers (5)

mgilson
mgilson

Reputation: 309909

In this context, I think it's important to know why inputs can't be zero? Typically, I think the way most programs will handle this is to raise an exception if a bad value is passed. Then the exception can be handled (or not) in the calling routine.

You'll often see it written "Better to ask forgiveness" as opposed to "Look before you leap". Of course, If you're often passing 0 into the function, then the try / except clause could get expensive (try is cheap, except is not).

If you're set on "looking before you leap", I would probably use the first form to keep indentation down.

Upvotes: 2

C&#233;dric Julien
C&#233;dric Julien

Reputation: 80761

First, you can use return without anything after, you don't have to force a return 0.

For the performance way, this question seems to prove you won't notice any difference (except if you're realy unlucky ;) )

Upvotes: 3

Christian Witts
Christian Witts

Reputation: 11585

Your second example will return after it displays the message box in this case.

I prefer to "return early" as in my opinion it leads to better readability. But then again, most of my returns that happen prior to the actual end of the function tend to be more around short circuiting application logic if certain conditions are not met.

Upvotes: 1

kindall
kindall

Reputation: 184171

In general, it improves code readability to handle failure conditions as early as possible. Then the meat of your code doesn't have to worry about these, and the reader of your code doesn't have to consider them any more. In many cases you'd be raising exceptions, but if you really want to do nothing, I don't see that as a major problem even if you generally hew to the "single exit point" style.

But why return 0 instead of just return, since you're not using the value?

Upvotes: 3

NominSim
NominSim

Reputation: 8511

I doubt the performance is going to be significantly different in either case. Like you I would tend to lean more toward the first method for readability.

In addition to the smaller indentation(which doesn't really matter much IMO), it precludes the necessity to read further for when your inputs == 0: In the second method one might assume that there is additional processing after the if/else statement, whereas the first one makes it obvious that the method is complete upon that condition.

It really just comes down to personal preference though, you will see both methods used in practice.

Upvotes: 1

Related Questions