Sobes.tech
Middle — Middle+

How to implement conditional blocks in an existing system that can be turned on or off without using standard mechanisms like Feature Toggle?

sobes.tech AI

Answer from AI

If standard Feature Toggle mechanisms cannot be used, conditional blocks can be implemented through configuration parameters or flags that are read at runtime and control the system's behavior.

Implementation options:

  • Configuration files: store flags in files (e.g., properties, YAML, JSON) that are loaded at application startup or dynamically.

  • Environment variables: use environment variables to enable/disable features.

  • Dynamic class loading: through reflection or DI, implementations can be swapped depending on settings.

  • Use of interfaces and strategies: inject dependencies that implement different behavior options, and select the required one based on the flag.

Example in Java with configuration:

public class FeatureManager {
    private Properties config;

    public FeatureManager() throws IOException {
        config = new Properties();
        try(InputStream in = new FileInputStream("features.properties")) {
            config.load(in);
        }
    }

    public boolean isFeatureEnabled(String featureName) {
        return Boolean.parseBoolean(config.getProperty(featureName, "false"));
    }
}

// Usage
FeatureManager fm = new FeatureManager();
if (fm.isFeatureEnabled("newPaymentFlow")) {
    // new code
} else {
    // old code
}

This approach allows enabling or disabling functionality without complex feature management systems, simply by changing the configuration and restarting the application (or implementing dynamic configuration loading).