Which is preferable: a function that decides itself that it should do something, or a decision to call this function?
function x() {
if ($userIsLoggedIn) {
alwaysHelloUser();
}
if ($visitorRegion != 'US') {
alwaysDisplayNoInternationalShipping();
}
if ($day == 'Sunday') {
alwaysLockDownStore();
}
}
function alwaysLockDownStore() {
exit("Sorry, we are closed!");
}
or
function y() {
perhapsSayHelloUser($user);
maybeDisplayNoInternationalShipping($region);
sometimesLockDownStore($day);
}
function sometimesLockDownStore($day) {
if ($day == 'Sunday') {
exit ("Sorry, we are closed!");
}
}
The example is trivial, but imagine that you enable functions using settings or permissions. Is there an accepted standard? My gut tells me to go after y()when there is something like exit()that upsets the normal flow. Otherwise, encapsulation and DRY will contribute x(). What do you think?
. . : , /, (, ) ( , )?