Consider this function intended to kill all running instances of a subprocess:
killAllFoobars()
{
pids = getRunningFoobars();
foreach ( pids as p ) {
killOneFoobar(p);
}
return TRUE;
}
Where should I check that in fact all Foobars were killed? Should killOneFoobar() check that its Foobar is now killed? Should killAllFoobars() check that there are no more running Foobars before returning TRUE (seems a bit disingenuous to return TRUE without checking). Should the function calling killAllFoobars() check?
Consider that the check is an expensive operation, so should not be done ‘just whenever’ but rather only when necessary.
4
You will never be sure that all the Foobars are killed (what if a new foobar is created after the check, but before returning from the function?). Just make sure that the killing functions work as intended. If you want to do more, you may loop forever, killing new processes as they are created.
1
Do a count on $pids = getRunningFoobars();
after the foreach
is finished to see if there are any that remain running. You can’t return a ‘true’ just assuming. It may be an expensive check, but the check is needed before you confirm to the user of your function that it did succeed.
3
If the expense of checking the kill of each foobar added up is equal to checking all of them, I would check in the deepest function, kill-one. If you were to reuse the kill-one function elsewhere it would be more accurate and useful. Also it consolidates your specific process logic into one function.
1