4

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

2
  • 1
    You want polymorphism. – Florian Margaine Jul 19 '13 at 15:55
  • @gnat That question seems different to me: their extended example checks many conditions, whereas I'm trying to eliminate many instances of the same (type of) condition check. – paul Jul 19 '13 at 18:16
4

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.

1
  • Looking things over, it definitely needs to be refactored (for the reason you mentioned). I should have seen this earlier -- guess I'm having an off day. – paul Jul 19 '13 at 18:28
1

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);
1
  • +1 A good first point and an interesting work-around. I don't think the work-around will work in my case though since not all the settings are in the same FeatureSettings class. – paul Jul 19 '13 at 17:31
1

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.

1
  • Good points to consider. The yes/no is always used with this method (so far). Also, it is a private method, so not externally visible. If I could make the function check the FeatureIsEnabled itself then I think I would probably go the route of polymorphism. – paul Jul 19 '13 at 17:39
0

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.