I am developing an application where part of the supported features may become optional in the future, so the user can enable/disable them. However I am not aware of an approach in handling such optional features in the code.

Is it just like putting checks to see if the feature is enabled by the user or not, before executing that part of the code,

if (isFeatureXEnabled)  
     //do something  
    //don't do it OR show some notification/dialog to the user that it is disabled  

or (hopefully) there is a better approach/design pattern that can be used?

  • 2
    There are may ways in which features can be optional. Depending on which way applies to your code base, the correct answer might be if statements, the Decorator pattern, unchanged code and varying configuration data, inheritance and abstract factories, etc. We have to have more detailed information to answer that. – Kilian Foth Aug 25 '17 at 13:45
  • 4
    @gnat: I hope that question is more similar than the vast disparity of the two question titles suggests. – Robert Harvey Aug 25 '17 at 15:20
  • 1
    @gnat I agree that my question can be solved with the answers provided in the linked question. – dragi Nov 10 '17 at 9:39

There are several approaches to this:

  • For simple and well isolated features, the simple flag could be a good approach.
  • For more complex optional processing, you could consider the strategy design pattern: this is ideally suited for alternative approaches in a general algorithm. "with" and "without" the option would be two different strategies.
  • in some cases, the use of a template method pattern could be a viable behaviour.

Finally, given your specific need, I'd strongly advise having a look at Martin Fowler's "feature toggle".

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