What is the most common usage for the data returned by the get_tags
function? I would assume at some point you're going to want to iterate over it. Consider the following:
Lists, tuples, and dicts
# returning a list
for tag in get_tags():
print tag
# returning None
tags = get_tags()
if tags is not None:
for tag in tags:
print tag
In the above example, returning None
is more tedious and less readable than returning an empty list since you have to check that tags
is a valid iterable before trying to iterate over it. An empty list is a valid iterable and does not need the check. Dictionaries and tuples are similar.
Strings
For strings, I find myself doing string manipulation or searching on a result that will most often return a string:
import re
description = commit.get_description().lower()
# get all JIRA issues
for project_name, issue_id in re.findall(r'(\w+)-(\d+)', s):
jira.close_issue(project_name, issue_id)
# vs.
description = commit.get_description()
if description:
description = description.lower()
for project_name, issue_id in re.findall(r'(\w+)-(\d+)', s):
jira.close_issue(project_name, issue_id)
Instances of Classes
It make sense to return None
for an instance of a class in cases where there is a test for existence.
# using xml.etree.ElementTree as an example,
# the .find() method of an XML element will return None if
# an Element is not found
my_tag = root.find('my-tag')
if my_tag is not None:
do_something_with_my_tag(my_tag)
Summary
The return value should implement all of the expected methods for normal usage, i.e. duck typing (except, of course, the last case where None
is usually better for instances).