6

[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>
mankand007
  • 952
  • 2
  • 10
  • 22
  • 3
    It depends on if you're a believer in the "single exit point" or not. In Python a single exit point might be particularly helpful in ensuring you're always returning the same type, which you've illustrated very well in your example. – Mark Ransom Jun 25 '12 at 17:23
  • Related: http://stackoverflow.com/questions/9191388/if-else-return-or-just-if-return – Wooble Jun 25 '12 at 17:25

5 Answers5

3

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?

kindall
  • 178,883
  • 35
  • 278
  • 309
3

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 ;) )

Community
  • 1
  • 1
Cédric Julien
  • 78,516
  • 15
  • 127
  • 132
  • 1
    Except that `None` which is returned from a bare `return` statement can have an effect on program execution is you're expecting an integer return value, ala 1980s programming. – Christian Witts Jun 25 '12 at 17:26
  • 1
    @ChristianWitts: Well, since i'm not expecting any return value from this function, a bare return will not have any issues, I suppose. Right? – mankand007 Jun 25 '12 at 17:47
  • 1
    @mankand007: That is correct, the return value would only have a potential effect on your program if you're expecting the value to be a certain type or within a certain range and end up getting something abnormal. – Christian Witts Jun 25 '12 at 17:51
2

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.

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • well, the `inputs == 0` is for illustrative purposes only. I'm writing a pyqt program, and it would make no sense to paste the actual code here, where i'm checking the value of a combo box. – mankand007 Jun 25 '12 at 17:33
  • @mankand007 -- The point is still the same. In python, if a function gets bad input, you expect it to raise an exception unless the authors have documented some return value to mean something bad happened. – mgilson Jun 25 '12 at 17:38
  • it's not a case of bad input. the combo box i'm checking is not an input to this function. I expect the user to fill that combo box before clicking the button which holds calls this function. Apologies if the example is misleading. – mankand007 Jun 25 '12 at 17:45
1

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.

NominSim
  • 8,447
  • 3
  • 28
  • 38
1

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.

Christian Witts
  • 11,375
  • 1
  • 33
  • 46