I have a bunch of calls to a method that may or may not need to be made depending on whether certain features are enabled or not. As such, I've wrapped these calls in if
blocks checking the enabled statuses.
The arguments to the method in each of these calls use some long variable names, so I've broken them out into different lines for readability / to adhere to our line-length coding standard.
These large method-call blocks, combined with the condition checks to see if they should be run (exacerbated by the mandate that we enclose all if
blocks in braces), are quite noisy.
So I have something like this:
if (FeatureSettings.FeatureXIsEnalbed)
{
this.TheMethodImCalling(
FeatureSettings.ReallyLongFeatureXSettingAName,
FeatureSettings.ReallyLongFeatureXSettingBName,
...);
}
if (FeatureSettings.FeatureYIsEnalbed)
{
this.TheMethodImCalling(
FeatureSettings.ReallyLongFeatureYSettingAName,
FeatureSettings.ReallyLongFeatureYSettingBName,
...);
}
if (FeatureSettings.FeatureZIsEnalbed)
{
this.TheMethodImCalling(
FeatureSettings.ReallyLongFeatureZSettingAName,
FeatureSettings.ReallyLongFeatureZSettingBName,
...);
}
To reduce the noise and redundancy in this code I was thinking of taking the if
block structure and putting it inside TheMethodImCalling
, then passing in the ...IsEnabled
value along with the rest of the arguments.
This seems somewhat silly though as I would effectively be calling the method and telling it "Don't do anything." when a feature wasn't enabled.
Now, I could probably make the code a bit more readable by assigning the long settings to shorter temporary variables or even fields of an Arguments
class that I would then pass to the method, but those don't address the core question:
Should I repeat the condition checking code or put it in the function?
Or is there an alternative I should consider? (Both options seem somewhat smelly...)