{
    "slug": "transaction_script",
    "term": "Transaction Script Pattern",
    "category": "quality",
    "difficulty": "intermediate",
    "short": "A pattern where each business operation is a single procedure — simple, linear, and appropriate for straightforward workflows without complex domain logic.",
    "long": "Transaction Script (Martin Fowler, PoEAA) organises logic as a set of procedures — one function per business transaction. The procedure reads data, applies logic, and writes results. Simple, easy to understand, easy to trace. Best for: simple CRUD applications, scripting, reporting, and ETL. Problems at scale: duplicate logic across scripts, difficult to reuse, logic grows unwieldy as rules multiply. The contrast with Domain Model: Transaction Script = logic in procedures; Domain Model = logic in objects.",
    "aliases": [
        "procedural design",
        "script-based architecture",
        "stored procedure pattern"
    ],
    "tags": [
        "patterns",
        "architecture",
        "design"
    ],
    "misconception": "Transaction Script is always inferior to Domain Model — for simple workflows Transaction Script is cleaner and easier to understand than a full domain model; choose based on actual complexity.",
    "why_it_matters": "Applying Domain Model to a simple CRUD app is over-engineering; applying Transaction Script to a complex billing system produces an unmaintainable tangle of duplicated logic — matching pattern to complexity is the skill.",
    "common_mistakes": [
        "Sticking with Transaction Script as the domain grows — recognise when duplication signals time to introduce a domain model.",
        "Long transaction scripts with 200+ lines — extract helpers but consider whether a domain model is needed.",
        "Business logic split between scripts and stored procedures — consolidate in one place.",
        "No service layer abstraction — transaction scripts directly calling SQL make testing difficult."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "domain_model_pattern",
        "active_record_pattern",
        "service_worker",
        "anemic_domain_model"
    ],
    "prerequisites": [
        "single_responsibility",
        "data_mapper_vs_active_record",
        "domain_driven_design"
    ],
    "refs": [
        "https://www.martinfowler.com/eaaCatalog/transactionScript.html"
    ],
    "bad_code": "// Transaction script — appropriate for this simplicity:\nfunction createUser(array $data): int {\n    $pdo->prepare('INSERT INTO users (name, email) VALUES (?, ?)')\n        ->execute([$data['name'], $data['email']]);\n    return $pdo->lastInsertId();\n}\n// Fine for simple CRUD — no need for domain model complexity here",
    "good_code": "// Transaction script becoming unwieldy — time to refactor to domain model:\nfunction processOrder(int $orderId): void {\n    // 50 lines: fetch order, check inventory, apply discount rules,\n    // calculate tax, charge payment, update inventory, send email,\n    // update loyalty points, generate invoice...\n    // Each rule duplicated in processReOrder(), processSubscriptionOrder()...\n    // Signal: extract Order domain object with these methods\n}",
    "quick_fix": "Use Transaction Script for simple operations where Domain Model is overkill — a PHP function that handles one use case from start to finish is fine for CRUD; refactor to Domain Model when business rules grow complex",
    "severity": "info",
    "effort": "low",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/transaction_script",
        "html_url": "https://codeclaritylab.com/glossary/transaction_script",
        "json_url": "https://codeclaritylab.com/glossary/transaction_script.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Transaction Script Pattern](https://codeclaritylab.com/glossary/transaction_script) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/transaction_script"
            }
        }
    }
}