I often have to execute some function, save the output, and depending on what the output was, do different things. And then later on use the output as input to more functions.
It results in a very specific pattern of code.
I feel like I'm missing something simple. ... as if some logic here could be somehow streamlined. The variable declaration, variable checking, and forthcoming execution of other stuff onto said variable seems almost redundant.
(tried to make the code as applicable to everyone and as applicable to every application, as possible) here's some sample code that fits this predicament:
some_sort_of_data=validate(var1, 'type1')
if some_sort_of_data.is_bad:
return some_sort_of_data.is_bad
more_data=validate(var2, 'type2')
if more_data.is_bad:
return more_data.is_bad
get_special_info=get_parent_of_item(some_sort_of_data.validated_item)
if get_special_info['error']:
return get_special_info['data']
if not (has_access_rights() or
special_relationship(more_data.validated_item) or
some_sort_of_other_permissions(get_special_info['result'])):
return "Blah Blah blah some sort of incompatibility"
new_status_result=change_status(some_sort_of_data.validated_item, more_data.validated_item)
if new_status_result['error']
return new_status_result['result']
# If we've passed all the previous checks and managed to get all the way here..
return "yay did a thing successfully"
... is it just me or do you also see how such a pattern feels off? If so, how can it best be streamlined?
edit: I know in some other languages you can streamline it a bit by writing
if (some_sort_of_data=validate(var1, 'type1')=="success")
return some_sort_of_data.some_class_variable
instead of
some_sort_of_data=validate(var1, 'type1')
if some_sort_of_data=="success":
return some_sort_of_data.some_class_variable
but that's still not enough and still quite redundant, right?