Should I repeat condition checking code or put it in a function? [duplicate]

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

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

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

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

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Should I repeat condition checking code or put it in a function? [duplicate]

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

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

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

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

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật