← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

Refactoring Boolean Flag Parameters

Code Quality Intermediate
debt(d5/e5/b5/t5)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5). The detection_hints list phpmd and phpstan, both specialist static analysis tools that require configuration and intentional use. The code_pattern `bool $|boolean $` is detectable but not caught by default linters in typical setups — it requires running these tools deliberately.

e5 Effort Remediation debt — work required to fix once spotted

Closest to 'touches multiple files / significant refactor in one component' (e5). The quick_fix mentions replacing boolean params with enums or separate functions, extracting options into value objects for 3+ flags — these changes ripple through all call sites of the affected function, touching multiple files and requiring coordinated updates across callers and the function definition itself.

b5 Burden Structural debt — long-term weight of choosing wrong

Closest to 'persistent productivity tax' (b5). The term applies_to web, cli, and queue-worker contexts — broad reach. Boolean flags in APIs impose ongoing cognitive load on every developer reading or extending those functions, slowing down work streams that touch those APIs. However, it's not quite architectural (b7) since it's function-level rather than system-shaping.

t5 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'notable trap' (t5). The misconception is explicit: developers believe PHP 8 named arguments fix the underlying design problem, but they only improve call-site readability — the boolean flag design issue persists. This is a documented gotcha that many developers learn, matching the t5 anchor of a notable documented trap most devs eventually encounter.

About DEBT scoring →

TL;DR

Boolean flags as function parameters obscure intent — replace with enums, separate functions, or named options objects for self-documenting APIs.

Explanation

function createUser($name, true, false, true) — what do those booleans mean? Boolean parameters (flag arguments) make call sites unreadable. Refactoring options: (1) Replace with enums/named constants, (2) Split into separate functions (createAdminUser, createGuestUser), (3) Use options object/named arguments, (4) Builder pattern for complex construction. PHP 8.0 named arguments partially solve the readability issue at call sites but don't fix the function signature. Martin Fowler's 'Remove Flag Argument' refactoring addresses this systematically.

Common Misconception

Named arguments in PHP 8 fix boolean flag problems — they improve call-site readability but the underlying design issue (boolean flags) remains.

Why It Matters

Boolean flag parameters are one of the most common sources of unreadable code — a function with 3 boolean params has 8 possible behaviours, impossible to understand without docs.

Common Mistakes

  • Adding boolean params to extend existing functions instead of adding new ones.
  • Using boolean to toggle completely different behaviour — should be separate functions.
  • Not using named arguments (PHP 8) at minimum to document existing boolean params.

Code Examples

✗ Vulnerable
createUser('Paul', true, false, true); // What are these booleans?

function createUser(string $name, bool $isAdmin, bool $sendEmail, bool $active) {}
✓ Fixed
// Option 1: Named enum
enum UserRole { case Admin; case Guest; }
createUser('Paul', UserRole::Admin);

// Option 2: Separate functions
createAdminUser('Paul');
createGuestUser('Paul');

// Option 3: Options object
createUser('Paul', new UserOptions(role: UserRole::Admin, active: true));

Added 23 Mar 2026
Views 58
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 2 pings F 1 ping S 3 pings S 1 ping M 0 pings T 2 pings W 0 pings T 1 ping F 0 pings S 0 pings S 0 pings M 0 pings T 1 ping W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 1 ping T 1 ping W
SEMrush 1
PetalBot 1
Scrapy 8 Amazonbot 7 ChatGPT 5 Perplexity 5 Google 4 Ahrefs 4 Unknown AI 3 Majestic 1 Claude 1 Bing 1 Meta AI 1 PetalBot 1 SEMrush 1
crawler 39 crawler_json 2 pre-tracking 1
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Medium
⚡ Quick Fix
Replace boolean params with enums or separate functions. Use PHP 8 named arguments at minimum. Extract options into a value object for 3+ related flags.
📦 Applies To
web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
bool \$|boolean \$
Auto-detectable: ✓ Yes phpmd phpstan
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Medium Context: Function Tests: Update


✓ schema.org compliant