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

    if get_special_info['error']:
        return get_special_info['data']

    if not (has_access_rights() or
        special_relationship(more_data.validated_item) or 
            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?


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
    Aug 5, 2019 at 14:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.