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...)

有帮助吗?

解决方案

Usually when something feels awkward like that, it's because you need to refactor your objects to more logically allocate the responsibilities. If you have a bunch of these, my personal preference would be to put them in a list, and call them like this:

for feature in enabledFeatures:
    feature.process()

That way you're not even wasting a conditional check on disabled features, which is especially useful when your ratio of disabled to enabled features is high.

其他提示

You may not have a choice here: if evaluating the ReallyLongFeatureZSettingAName property when FeatureSettings.FeatureZIsEnalbed equals false results in throwing an exception, the idea of passing the "guarding" FeatureSettings.FeatureZIsEnalbed to TheMethodImCalling will trigger an exception:

this.TheMethodImCallingIfEnabled(
    FeatureSettings.FeatureZIsEnalbed,
    FeatureSettings.ReallyLongFeatureZSettingAName,
    FeatureSettings.ReallyLongFeatureZSettingBName,
    ...);

Depending on your target language, you may be able to shorten your code by "inverting" the call sequence: for example, in C# you could add this method to FeatureSettings

class FeatureSettings {
    ...
    public void RunIfEnabledZ(Func<string,string> callback) {
        if(FeatureZIsEnalbed) {
            callback(ReallyLongFeatureZSettingAName, ReallyLongFeatureZSettingBName);
        }
    }
}
...
var theCallback = (a, b) =>  this.TheMethodImCalling(a, b, ...);
FeatureSettings.RunIfEnabledX(theMethod);
FeatureSettings.RunIfEnabledY(theMethod);
FeatureSettings.RunIfEnabledZ(theMethod);

There is nothing wrong with a guard condition at the top of a function, but I would consider two things first: (1) what is the normal use case -- does it normally get called with yes or no? And then (2) is it externally visible? Can they do the same kind of check?

Calling a method which does nothing 99% of the time, is a bad idea -- it's misleading.

If your function is externally exposed, and called from elsewhere your condition may not make sense, and then they just always pass true.

It would be better if your function could calculate FeatureIsEnabled itself.

In layman's words:

  • The code seems ok to me.
  • It has a very small cyclomatic complexity.
  • I see no noise in having to close the if's curly brackets.
  • Having simple sequential "if" blocks *is not a code smell*.

But if you thing the list of conditional proccesses will be growing in the future you should be considering the "Command" or "Chain of responsability" design patterns. That way you will not need the ifs with their corresponding curly braces that bother you so much.

许可以下: CC-BY-SA归因
scroll top