Fundamentals 6 min read

Simplifying Driver Assignment Logic: Guard Clauses, Decision Tables, and Function Composition

The article shows how to replace the taxi‑hailing app’s deeply nested driver‑assignment if‑statements with early‑exit guard clauses, data‑driven decision tables, and composable functions, thereby flattening the logic, enhancing readability, simplifying testing, and making future extensions easier to implement.

Java Tech Enthusiast
Java Tech Enthusiast
Java Tech Enthusiast
Simplifying Driver Assignment Logic: Guard Clauses, Decision Tables, and Function Composition

This article discusses how to refactor the complex, nested if logic used in a taxi‑hailing app's driver assignment module, making the code clearer and easier to maintain.

1. Guard Clauses

Guard clauses check error conditions early and exit the function immediately, reducing nesting depth.

def divide(a, b):
    if b == 0:
        raise ValueError("Cannot divide by zero")
    return a / b

2. Decision Tables

Decision tables represent complex branching as data, allowing the engine to evaluate rules without deep if‑else chains.

def evaluate_decision_table(table, context):
    for rule in table:
        if all(context.get(k) == v for k, v in rule['conditions'].items()):
            return rule['action']
    return None

# Define decision table
decision_table = [
    {'conditions': {'age': 18, 'has_license': True},  'action': 'Drive'},
    {'conditions': {'age': 18, 'has_license': False}, 'action': 'Learn to drive'},
    {'conditions': {'age': 17, 'has_license': True},  'action': 'Cannot drive'}
]

context = {'age': 18, 'has_license': True}
action = evaluate_decision_table(decision_table, context)
print(action)  # Output: Drive

3. Function Composition

Function composition builds new behavior by chaining simple functions, eliminating the need for explicit conditional blocks.

def add(x, y):
    return x + y

def multiply(x, y):
    return x * y

def add_and_multiply(x, y, z):
    return multiply(add(x, y), z)

result = add_and_multiply(1, 2, 3)  # Output: 18
const add = (x, y) => x + y;
const multiply = (x, y) => x * y;
const addAndMultiply = (x, y, z) => multiply(add(x, y), z);
const result = addAndMultiply(1, 2, 3);  // Output: 9

Applying guard clauses, decision tables, and function composition flattens the original five‑level nested logic, improving readability, testability, and future extensibility.

JavaScriptpythonrefactoringdecision tablesfunction compositionguard clauses
Java Tech Enthusiast
Written by

Java Tech Enthusiast

Sharing computer programming language knowledge, focusing on Java fundamentals, data structures, related tools, Spring Cloud, IntelliJ IDEA... Book giveaways, red‑packet rewards and other perks await!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.