Overview
This guide covers advanced techniques for building sophisticated, performant, and maintainable rule systems. These patterns are for experienced users tackling complex business logic.Nested Operations in Outcomes
Outcomes can contain operations or literal data structures, enabling sophisticated conditional logic.Pattern: Outcome as Operation
When an outcome is an object withoperator and input, it’s evaluated as an operation:
- Multi-level conditional calculations
- Different calculation methods based on type
- Dynamic threshold evaluation
- Cascading business logic
Pattern: Outcome as Literal Array
When an outcome is an array of data objects, it returns that literal array:- Dynamic UI menus or navigation
- Permission-based action lists
- Workflow step definitions
- Configuration objects per user type
- Form field definitions
Important Distinction
Combining Both Patterns
You can mix these patterns in a single rule set:- Calculate a value using operations (eligibleForBonus)
- Return structured data based on that calculation (bonusDetails)
Complex Multi-Stage Calculations
Break down complex calculations into logical stages for maintainability and debuggability.Pattern: Staged Pipeline Processing
Process data through multiple transformation stages. There are several approaches to organizing and documenting complex multi-stage rules:Approach 1: Self-Documenting Rule Names (Recommended)
Use descriptive prefixes in rule names to indicate stage and purpose:Approach 2: Inline Comments within Rules
Add a_comment field inside individual rules for complex logic:
The
_comment field is ignored by the Rules engine but stays with the rule definition. Use underscore prefix to indicate it’s metadata.Approach 3: Separate Rules Steps (For Very Complex Logic)
Split stages into separate Rules steps in your flow:1
Rules Step 1: Extract & Validate
Extract raw data and validate inputs
2
Rules Step 2: Apply Business Logic
Apply multipliers and transformations
3
Rules Step 3: Aggregate & Filter
Calculate totals and filter results
4
Rules Step 4: Classify
Determine final classification
- Each stage has 20+ rules
- Stages need independent testing
- Different team members own different stages
- Need to conditionally skip stages based on earlier results
- Clear separation of concerns
- Easy to test individual stages
- Simple to add new stages
- Obvious where problems occur
Performance Optimization
Techniques for optimizing rule execution speed and memory usage.Cache Expensive Calculations
Store results of expensive operations and reuse them: ❌ Inefficient (recalculates):Minimize Array Iterations
Extract all needed properties in one pass: ❌ Multiple iterations:Use Efficient Operators
Choose operators optimized for your use case:| Instead of | Use | Why |
|---|---|---|
Multiple >= checks | between with inclusivity | Single operation vs multiple comparisons |
and with many inputs | Pre-filter with intermediate rules | Reduces complexity |
Nested if operators | Conditional rule format | Optimized evaluation path |
jPath for simple access | Direct reference | Avoids JSON parsing overhead |
Early Exit Patterns
Structure conditions to fail fast:Put cheap checks (like
notEmpty) before expensive ones (like complex calculations). The and operator short-circuits on first false.Advanced Array Processing
Sophisticated techniques for working with collections.Parallel Array Processing
Process multiple related arrays simultaneously:Nested Data Extraction
Extract from deeply nested structures:Dynamic Array Generation
Build arrays based on complex conditions:Complex Business Logic Patterns
Advanced patterns for sophisticated decision-making.Multi-Dimensional Scoring
Score based on multiple independent dimensions:State Machine Implementation
Implement state transitions with validation:Error Handling & Validation
Defensive programming techniques for robust rules.Comprehensive Input Validation
Validate all inputs before processing:Safe Division & Null Handling
Avoid division by zero and null references:Graceful Degradation
Provide fallback values when data is unavailable:Testing Strategies
Approaches for thoroughly testing complex rule systems.Test Data Sets
Create comprehensive test scenarios:Incremental Testing
Test rules progressively:1
Test individual rules
Start with simple calculations, verify each rule independently
2
Test rule chains
Verify rules that depend on other rules
3
Test conditions
Check all condition branches execute correctly
4
Test edge cases
Verify boundary conditions, nulls, empty arrays
5
Test integration
Verify rules work with variable mapping from previous steps
Maintainability Best Practices
Techniques for keeping rule systems maintainable as they grow.Naming Conventions
Establish consistent naming patterns that make rules self-documenting:extracted_*- Data extraction operationsis_*orhas_*- Boolean validation checkstotal_*,*_amount,*_rate- Calculated valuesshould_*,can_*- Decision flags- Plural nouns for arrays (
eligible_items,validation_errors) - Use underscores for readability:
total_amountnottotalAmount
Logical Grouping
Organize related rules using consistent prefixes: Option 1: Stage Prefixes- Step 1 - Validation Rules: Input validation and error checking
- Step 2 - Calculation Rules: Core business calculations
- Step 3 - Formatting Rules: Output formatting and display values
Documentation Within Rules
For complex logic that needs explanation, add inline documentation: Using _comment field:- Documentation stays with the rule (not separated)
- Can include metadata like owner, last updated, related rules
- Underscore prefix indicates these are metadata fields
- Easy to see documentation when viewing/editing rules
Migration Strategies
Moving existing business logic to Rules.From Spreadsheet Formulas
Convert Excel formulas to rules: Excel:From Code Logic
Convert programmatic logic: JavaScript:What’s Next?
Examples Library
See these techniques applied in complete real-world examples
Technical Reference
Deep dive into performance characteristics and limits
Rule Patterns
Review fundamental patterns before tackling advanced techniques
Operations Reference
Complete reference for all available operators
Need Help? Visit our Help Center or join the Community for support.