Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Shiitchan can't be shittier

Name: Anonymous 2010-08-31 8:10

Discuss.

Name: Anonymous 2010-09-01 4:56

Xarn, my boy.

    if resp.getheader('Content-Encoding') == 'gzip':
        return gzip.GzipFile(fileobj=StringIO(resp.read()))
    else:
        return resp


Why would you do this? You know that the else is being taken by virtue of the control flowing to that point, making the else completely superfluous and unnecessary.

Plus, supposing you decide to change the else to an if (since we've established that else if is utterly pointless), it's possible that there's no return statement at all in the function because both of the return branches won't be taken.

Name: Anonymous 2010-09-01 5:50

>>30
Implying any language allows the function to finish after a return statement is reached

Name: Anonymous 2010-09-01 8:32

>>32
Thanks for being deliberately obtuse.

Even if an exception does pop up in
    if resp.getheader('Content-Encoding') == 'gzip':
        return gzip.GzipFile(fileobj=StringIO(resp.read()))

the else is still useless and that, in fact, proves my point that there should be a return value outside of any if scope in the function.

Consider:
    try:
        if resp.getheader('Content-Encoding') == 'gzip': return gzip.GzipFile(fileobj=StringIO(resp.read()))
           else: return resp
    except: pass


There is no return value if an exception is thrown there. I'm not sure how Python treats this case (of assignment to a function with no return value), but it probably assigns the value of the variable to None.

The overarching point is that there's never a good reason to do the if return else return idiom; maintainability and clarity both increase (maintainability in multiple ways) when you avoid using that idiom and it doesn't cost anything. In fact, using it costs those things.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List