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?
-
Still not enough by what measurement? Is there a requirement for code compactness that's not being met?Blrfl– Blrfl2019年08月01日 19:58:16 +00:00Commented Aug 1, 2019 at 19:58
-
1Possible duplicate of Style for control flow with validation checksgnat– gnat2019年08月01日 20:15:26 +00:00Commented Aug 1, 2019 at 20:15
1 Answer 1
Without knowing the details of your logic I would say you would be better served using exception handling instead. This would be appropriate if the normal/desired program flow would mean no bad data and you would want to abort in case you were to encounter some bad data.
If you only want to report bad data and continue, you may just log/report in your verifying function and not check anything at the caller's side.
-
It seems to boil down to part of my if-clauses being validation, part of my if-clauses being actual branches to show the user different things, and part of my if-clauses being yet another non-error-harvesting item. It seems that the true answer to my question is a combination of three things - exception collecting, restructuring a few items as methods instead of as functions, and using early
return
clauses in a sub-function of the parent function to create multiple valid output data pieces. I'll accept your answer as correct.cake– cake2019年08月05日 14:47:43 +00:00Commented Aug 5, 2019 at 14:47