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?
Written by Rodrigo Espinosa
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Python
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#