Skip to content

Logical Operators

Overview

Logical operators in the expression language allow you to combine multiple conditions into complex boolean expressions. The language supports three fundamental logical operations:

  • and - Logical AND operator
  • or - Logical OR operator
  • Parentheses () - For grouping and controlling precedence

Core Concepts

1. Basic Operators

AND Operator

The and operator returns true only if both operands evaluate to true.

js
// Both conditions must be true
age > 18 and status == "active"

// Multiple AND conditions
category == "premium" and price > 100 and stock > 0

Truth table for AND:

Left    Right   Result
true    true    true
true    false   false
false   true    false
false   false   false

OR Operator

The or operator returns true if at least one of the operands evaluates to true.

js
// Either condition can be true
status == "active" or status == "pending"

// Multiple OR conditions
priority == "high" or severity == "critical" or @isUrgent(task)

Truth table for OR:

Left    Right   Result
true    true    true
true    false   true
false   true    true
false   false   false

2. Operator Precedence

The expression language follows a strict operator precedence order:

  1. Parentheses ()
  2. NOT
  3. AND
  4. OR

This means that:

js
// This expression:
status == "active" and role == "admin" or department == "IT"

// Is evaluated as:
(status == "active" and role == "admin") or department == "IT"

3. Expression Grouping

Parentheses can be used to:

  • Override default operator precedence
  • Group related conditions
  • Create clear, readable expressions

Examples:

js
// Default precedence
age > 18 and role == "user" or status == "special"
// Evaluates as: (age > 18 and role == "user") or status == "special"

// Modified precedence with parentheses
age > 18 and (role == "user" or status == "special")
// Evaluates as specified by the parentheses

4. Complex Expressions

You can create sophisticated logical conditions by combining operators:

js
// Multiple conditions with different operators
(status in ("active", "pending")) and 
(priority > 3 or @isUrgent(task))

// Nested conditions
((age > 18 and role == "user") or 
 (age > 21 and role == "admin")) and 
 status not in ("blocked", "deleted")

// Combining with functions
@hasPermission(user, "edit") and 
(status == "draft" or @isOwner(user, document))

Implementation Details

Evaluation Process

  1. The expression is parsed into a binary tree structure where:

    • Leaf nodes are individual conditions
    • Internal nodes are logical operators (AND/OR)
    • Each node implements the Evaluate() method
  2. Evaluation follows these rules:

    • Short-circuit evaluation is used for efficiency
    • For AND: if left operand is false, right is not evaluated
    • For OR: if left operand is true, right is not evaluated

Error Handling

The evaluation process handles several types of errors:

  1. Syntax Errors:

    js
    // Missing right operand
    status == "active" and
    
    // Invalid operator combination
    status == "active" and and role == "admin"
  2. Type Errors:

    js
    // Mixing incompatible types
    age > "18" and status == true  // age should be compared with number
  3. Evaluation Errors:

    js
    // Undefined field access
    nonexistent.field == "value" and status == "active"

Best Practices

  1. Use Parentheses for Clarity

    js
    // Clear intention
    (status == "active" and priority > 3) or 
    (status == "pending" and @isUrgent(task))
  2. Break Complex Expressions

    js
    // Instead of one long expression:
    (status == "active" and priority > 3) or 
    (status == "pending" and @isUrgent(task)) or 
    (category == "special" and @hasFlag(item))
    
    // Consider splitting into multiple conditions
    isHighPriority = status == "active" and priority > 3
    isPending = status == "pending" and @isUrgent(task)
    isSpecial = category == "special" and @hasFlag(item)
    
    isHighPriority or isPending or isSpecial
  3. Leverage Short-Circuit Evaluation

    js
    // Put cheaper operations first
    @isSimpleCheck(item) and @expensiveOperation(item)
  4. Maintain Readability

    js
    // Instead of
    status=="active"and priority>3or category=="urgent"
    
    // Write
    status == "active" and priority > 3 or category == "urgent"

Common Patterns

  1. Permission Checking

    js
    @hasRole(user, "admin") or 
    (@hasRole(user, "editor") and status == "draft")
  2. Status Validation

    js
    status in ("active", "pending") and 
    (priority > 3 or @hasFlag(item, "urgent"))
  3. Complex Business Rules

    js
    (category == "premium" and price > 100) and
    (stock > 0 or @isPreorder(item)) and
    not @isRestricted(item)