是否应使用相同的数据类型从函数返回或分配默认值?还是没有?什么是更好的编码实践,为什么?
例如。python中的一些伪代码:
/1
def my_position(): # returns a positive integer if found
if(object is present):
position = get_position()
return position # eg 2,3,4,6
else:
return None # or return -1 or 0 ??
/2
def get_database_rows():
do query to whatever database
if(rows are found):
return [list of rows]
else:
return None # or return empty list [] ?
/3
the_dictionary = {'a' : 'john','b':'mike','c': 'robert' } # values are names i.e. non empty string
my_new_var = the_dictionary.get('z', None) # or the_dictionary.get('z','') ?