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

Refactoring Boolean Flag Parameters

quality Intermediate

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 27
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 2 pings M 0 pings T 1 ping W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Amazonbot 6 Perplexity 5 Unknown AI 3 Google 3 ChatGPT 2 Ahrefs 2 Majestic 1
crawler 21 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