Often times, through my framework's importing side of things, I have access to certain data that functions I'm about to use will certainly need themselves.
I have "resolvers" that these said functions can use to get the additional data they need but they're very heavy operations and I was thinking:
Since I already have access to that exact data the function would need, why not just pass it and not rely on these heavy-lifting resolvers to get it for me?
Meet "support arguments" - I can just pass these to an array and within my function, check if they're passed - if not, ask the resolvers to give them (since I need the data either way).
Here's my actual code:
/**
* Retrieves the component activated before a target component.
*
* @internal Targets the 'active_history' in the mother array, as such, it doesn't look at the installed history at all.
*
* @param string $target_component_name The target component name, used as a starting point.
* @param boolean $skip_check Flag to allow skipping of certain checks.
* @param array $support_package A support package passed from outside callers that can speed up the execution of this function due to bypasses of checks.
*
* @return string|mixed Returns the string of the component found before our target on success and a mix of boolean / WP_Errors on failure.
*/
public static function getActiveComponentBeforeThisOne( $target_component_name, $skip_check = False, $support_package = null )
{
$installed_components = get_option( 'currently_installed_demo_components' );
//Support check 1.
if( $skip_check == False ) {
if( !self::determineIfDemoComponentIsInstalled( $target_component_name ) ) {
return new \WP_Error(
'component-not-installed',
esc_html__( 'Component is not currently installed.', 'setup-theme' )
);
}
}
//Support check 2.
$local_package = [
'component_category' => ''
];
if( isset( $support_package['component_category'] ) ) {
$local_package['component_category'] = $support_package['component_category'];
} else {
$local_package['component_category'] = self::getComponentCategoryByName( $target_component_name );
}
//Do some things with $local_package['component_category'] here.
}
As you can see, the first support $skip_check
allows me to set a flag to skip a heavy check that can and is, in 95% of the cases, performed right before this function is called.
This is trap one: In an event-driven system such as WordPress (even if the hooks are perfectly done), one could hit race conditions failures, especially within AJAX calls (which is where these are used). My check would do nothing then. Even if in the 99.9% cases where it works, there's that dreadful situation where it doesn't - and it ends up breaking everything.
The second support is $support_package
. I am calling getActiveComponentBeforeThisOne
from a context where I already have access to a lot of data, specifically component_category
, so, for almost all cases, I can just provide it to my function, again, to skip these heavy checks that slow me down.
This is trap two: I could simply just decide to call getComponentCategoryByName
where I also call getActiveComponentBeforeThisOne
if, at that point, I see that component_category
is not available to me. This would introduce clarity as to what's going on. So the real issue here is visibility and the second, perhaps as big an issue is separation of concerns. As it stands, my getActiveComponentBeforeThisOne
function ends up doing way too much. A function that wrangels a lot of data is generally fine - it's not smart to separate, one-time use array operations into multiple functions because it quickly becomes a cluster-f to follow: good comments do just fine. But here, we clearly, here we have a monster function that does a lot in terms of checks, in fact, my functions would end up being 90%+ just these checks and the 10% actual "do thing X" functionality.
Am I just littering my code, crushing the data tier into my logic tier? What is the alternative?
It feels that, when I read my code, that I have to get through a lot of checks where I need to say "Ah, so it checks for this, this and this..." just to get to the actual "ah, it does this!".
I'm also dealing with very sensitive stuff where if I import / delete the wrong thing, my system fails and there's no other way to do it, so, checking at every step before running crucial operations is also needed.
$installed_components
variable? What kind of value is it? An array of strings? – Greg Burghardt Dec 14 '18 at 15:09$support_package
contain? – Greg Burghardt Dec 14 '18 at 15:41