I'm wondering where in a process should the control of a script exiting be placed?
If a function is used to determine whether a script should continue or not, should control, based on the result, be in the caller or the callee?
Are there scenarios where it could be in either?
(I'm sure this question has broader implications so please feel free to extend the answer to a higher level practice in programming. That would be great actually)
I will list some examples below to consider as options of conditional script exiting and how control could be delegated or not.
Imagine should_continue
is checking that a supplied arg is valid and its validity is required for the script to continue. Otherwise it exits.
'''
ex 1: return state to parent process to determine if script continues
'''
def should_continue(bool):
if bool:
return True
else:
return False
def init():
if should_continue(True):
print 'pass'
else:
print 'fail'
'''
ex 2: return state only if script should continue
'''
def should_continue(bool):
if bool:
return True
else:
print 'fail'
sys.exit() # we terminate from here
def init():
if should_continue(True):
print 'pass'
'''
ex 3: Don't return state. Script will continue if should_continue doesn't cause termination of script
'''
def should_continue(bool):
if not bool:
print 'fail'
sys.exit()
def init():
should_continue(True)
print 'pass'