Video in progress
Business rules change every week (discounts, limits). In code, do you choose the strategy pattern, configuration, a feature flag or a rules engine?
It depends on which part of the rule changes: if only numbers change, configuration/DB; if the structure of the logic changes, code (a strategy); if you need to roll it out gradually to certain users, a feature flag. I only take a rules engine when the business itself must write rules.
📖 Answer
The main question in the choice is who changes it, and how fast does it have to reach production?
| What changes | Solution | Cost |
|---|---|---|
| Only parameters (percent, limit, date) | a DB table + an admin panel | cheap, needs an audit |
| Logic, but developers write it | Strategy + deploy | reliable, with tests |
| Partial rollout of new logic | Feature flag + strategy | discipline in cleaning up flags |
| A business analyst writes rules themselves | A rules engine (Drools etc.) or a DSL | high: learning, testing, debugging |
Often the best solution is a mix: the logic in code, the parameters in data.
public interface DiscountRule {
boolean applies(Order o);
Money discount(Order o);
}
// the parameters come from the DB, the logic is in code
record MinTotalRule(Money threshold, Percent percent) implements DiscountRule { ... }
Rules engine risks:
- the rules end up outside version control and code review;
- the interaction between rules (what if A and B both fire?) becomes unclear;
- debugging and unit testing are hard;
- the promise that "the business writes it themselves" often doesn't work: in the end developers write them anyway, just in a worse language.
A rule for feature flags: every flag has an owner and a removal date. Hundreds of stale flags are a source of complexity and bugs in themselves.
Whichever way you go: write to an audit log who changed a rule and when, and record which rule version was applied to each order, to settle disputes later.
Sign up to mark studied questions and save your progress.