Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What design pattern can be used for feature toggle spaghetti?

The company that I work for has gone off the deep end with feature toggles-configuration keys that turn on/off particular behavior based on certain conditions. Martin Fowler actually refers to them as business toggles (http://martinfowler.com/bliki/FeatureToggle.html).

We have many clients all using the same service, but each wants slightly different behavior. Even worse, many want certain subgroups of their users to see different behavior. Thus, we use business toggles. The toggles have become a spaghetti ball of if/else logic that with occasionally unexpected interactions.

Are there any design patterns useful in managing a situation such as this?

Cheers!

like image 775
Kramer Avatar asked Sep 01 '15 20:09

Kramer


People also ask

When to use the feature toggle pattern?

Use the Feature Toggle pattern when Giving different features to different users. Rolling out a new feature incrementally. Switching between development and production environments. Credits Martin Fowler 29 October 2010 (2010-10-29). Designed by markusmo3⋅ © 2014-2021 Ilkka Seppälä

What is feature toggle extensibilitybehavioral?

Feature Toggle ExtensibilityBehavioral Also known as Feature Flag Intent Used to switch code execution paths based on properties or groupings. Allowing new features to be released, tested and rolled out. Allowing switching back to the older feature quickly if needed. It should be noted that this pattern, can easily introduce code complexity.

What are the toggles in the user interface used for?

These toggles are mainly used for testing purposes. The toggle splits the user base into different segments to expose an isolated segment to the new feature. The usage data for the segmented users are used to infer the effect of the new feature. As it’s only used for testing purposes, the toggles can be removed once the testing is done. 3.

What is the difference between toggle and flag in agile?

"Toggle" is a more appropriate name when code is turned on or off for a few major code branches. "Flag" is a more appropriate term if a decision point is followed by a very multi-conditional or broad set of codepaths. Applying feature toggles to your development process supports newer agile approaches.


1 Answers

  • The switches should be run-time switches, not compile-time ones. The reason is, that switches should be operable without re-deployment.
  • There are two possibilities to implement run-time conditional logic:
    1. if-then-else (or switch-case for multi-state switches)
    2. Polymorphy, which requires an object-oriented programming language.

I, personally, prefer to use plain if-then-else constructs, decorated with a comment saying TODO: Remove when feature has been tested. when applicable.

Keep in mind that every feature flag is a technical debt. Do not overuse them. I recommend against using them for business logic.


However, regarding your spaghetti ball of business toggles, I would recommend a refactoring.

  • No function should have a nesting depth larger than two. If it has, split into multiple functions with meaningful names.
  • Eliminate double negations.
  • De Morgan's laws are particularly useful for re-organizing conditional logic.

If that is not enough, you can model the business logic with a graph (e.g. decision flowchart) or a Matrix and use the respective standard algorithms.

like image 50
ManuelAtWork Avatar answered Sep 20 '22 14:09

ManuelAtWork