Last Updated: February 25, 2016
·
384
· rec

Multiple vs. one return point

The next block of codes are written on Python but the concept should be language agnostic.

With multiple return points:

if url.endswith('.json'):
    # Parser the JSON text to a python dictionary and return it
    return json.load(r.text)

elif url.endswith('.xml'):
     # Parse the XML text to a python dictionary and return it
     return xmltodict.parse(r.text)

# Return the plain text
return r.text

With only one return point:

if url.endswith('.json'):
    # Parser the JSON text to a python dictionary
    response =  json.load(r.text)

elif url.endswith('.xml'):
     # Parse the XML text to a python dictionary
     response = xmltodict.parse(r.text)

else: 
    # Return the plain text
    response = r.text

return response

The question should be, which block of code looks better?

Recommend
Say Thanks
Update Notifications Off
Respond