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

ABC Metric (Assignments, Branches, Conditions)

quality PHP 5.0+ Advanced

Also Known As

ABC score Assignment Branch Condition metric

TL;DR

Code complexity metric using vector magnitude of assignments, branches, and conditions — more expressive than line count.

Explanation

The ABC metric (Jerry Fitzpatrick, 1997) measures method complexity using three independent counts: A = assignments (state changes), B = branches (function/method calls and control transfers), and C = conditions (boolean expressions). The final score is computed as the vector magnitude: sqrt(A^2 + B^2 + C^2). Unlike cyclomatic complexity, which focuses on decision paths, ABC captures how much a method mutates state and delegates work. High A suggests excessive state manipulation, high B indicates heavy delegation or deep call chains, and high C reflects complex logic. Tools like PHPMetrics and PHPLoc report ABC per method. Scores above ~20 should be reviewed; above ~50 typically indicate methods that are difficult to test, reason about, and maintain.

Common Misconception

ABC metric and cyclomatic complexity measure the same thing. Cyclomatic complexity counts independent execution paths, while ABC measures state changes, calls, and conditions — they highlight different risks.

Why It Matters

High ABC scores correlate with fragile, hard-to-test code — reducing ABC improves readability, lowers defect rates, and simplifies refactoring.

Common Mistakes

  • Reducing ABC by splitting methods without reducing actual logic complexity.
  • Ignoring high assignment counts — excessive state mutation is a major maintainability risk.
  • Treating all branches equally — external calls and control flow both increase B but have different implications.
  • Using ABC thresholds blindly without considering domain complexity.

Avoid When

  • Evaluating very small methods where ABC adds little insight.
  • Comparing across different domains without normalising expectations.

When To Use

  • Identifying overly complex or state-heavy methods.
  • Enforcing maintainability thresholds in CI pipelines.

Code Examples

💡 Note
Refactoring reduces assignments and conditions per method, lowering ABC while improving readability and testability.
✗ Vulnerable
// ❌ High ABC — excessive state + branching
function processOrder($order, $user, $coupon, $tax) {
    $total = 0; $discount = 0; $shipping = 0;

    if ($order && $user && $coupon) {
        if ($coupon->valid && $coupon->amount > 0) {
            $discount = $coupon->amount;
        }

        $total = $order->subtotal - $discount + $tax + $shipping;
    }

    return $total;
}
✓ Fixed
// ✅ Lower ABC via decomposition and reduced state
function processOrder($order, $user, $coupon, $tax) {
    if (!$this->isProcessable($order, $user, $coupon)) {
        return 0;
    }

    $discount = $this->calculateDiscount($coupon);

    return $this->calculateTotal($order, $discount, $tax);
}

private function isProcessable($order, $user, $coupon): bool {
    return $order && $user && $coupon;
}

private function calculateDiscount($coupon): float {
    return ($coupon->valid && $coupon->amount > 0)
        ? $coupon->amount
        : 0;
}

private function calculateTotal($order, $discount, $tax): float {
    return $order->subtotal - $discount + $tax;
}

Added 15 Mar 2026
Edited 27 Mar 2026
Views 61
Rate this term
5.0 (1 rating)
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 1 ping T 2 pings F 0 pings S 0 pings S 1 ping M 0 pings T 1 ping W 0 pings T 3 pings F 0 pings S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Amazonbot 15 Perplexity 14 ChatGPT 4 Google 4 Unknown AI 3 Ahrefs 3 Majestic 2 Claude 1
crawler 44 crawler_json 2
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Medium
⚡ Quick Fix
Keep ABC < 20 per method; reduce assignments and condition chains via extraction and delegation.
📦 Applies To
PHP 5.0+ web cli queue-worker
🔍 Detection Hints
function with many assignments (=), conditions (if/&&/||), and method calls
Auto-detectable: ✓ Yes phpmetrics phploc phpmd
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: Medium Context: Function Tests: Update

✓ schema.org compliant