compact-guard-clauses

compact-guard-clauses is a skill for Claude Code from alivirgo/Major-AI-Skills. It costs 26 tokens per session (1,269 once invoked), scanned A, original, MIT.

A coding method that checks failure conditions early and returns immediately instead of placing the main logic inside many nested if statements.

In plain words
What is it for?
Use it when refactoring functions with several validation or error checks into clearer control flow.
Why use it?
It reduces deeply indented code, making the normal path easier to read and later changes easier to review.

Skill for Claude Code

Written for Claude Code: shipped in a Claude Code plugin. Also seen: mentions Codex.

Part of the major-ai-skills plugin — 147 skills, 8 plugins shipped together

Good fit Use it when refactoring functions with several validation or error checks into clearer control flow.

Compare 6 skills from other repositories ↓
Install with agentmods
npx agentmods add skills/alivirgo/major-ai-skills/compact-guard-clauses
Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

Any agent
npx skills add alivirgo/Major-AI-Skills --skill compact-guard-clauses
Clone the repo
git clone --depth 1 https://github.com/alivirgo/Major-AI-Skills

Made for: Claude Code.

Or install major-ai-skills, the plugin that ships this one along with the rest of its 147 skills, 8 plugins.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for compact-guard-clauses

README.md
[![agentmods](https://agentmods.dev/badge/skills/alivirgo/major-ai-skills/compact-guard-clauses/github.svg)](https://agentmods.dev/skills/alivirgo/major-ai-skills/compact-guard-clauses)
Your own site
<a href="https://agentmods.dev/skills/alivirgo/major-ai-skills/compact-guard-clauses"><img src="https://agentmods.dev/badge/skills/alivirgo/major-ai-skills/compact-guard-clauses/github.svg" alt="Measured on agentmods" height="20"></a>

Or the 80×15 button, for a site that already has a row of RSS and ATOM ones. Only the verdict fits; the numbers stay here.

agentmods 80×15 button for compact-guard-clauses

Your own site · 80×15
<a href="https://agentmods.dev/skills/alivirgo/major-ai-skills/compact-guard-clauses"><img src="https://agentmods.dev/badge/skills/alivirgo/major-ai-skills/compact-guard-clauses.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 26 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 1,269 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00026 $0.01269
Opus 5 $0.00013 $0.00634
Sonnet 5 $0.00005 $0.00254
Haiku 4.5 $0.00003 $0.00127

Measured yesterday against content hash b1cfb52f51e4, method: parsed. Prices are Anthropic first-party input rates as of 2026-09-12, from the pricing page.

Security

Grade A, and why

compact-guard-clauses scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

skills/compact-guard-clauses/SKILL.md · 126 lines

How it starts

The opening of the file, as written. The whole thing — 126 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Compact Guard Clauses (Early-Return Pattern)

Overview

Default AI code generation often falls into the "Pyramid of Doom" anti-pattern: nesting execution logic 4 to 6 levels deep inside cascading if/else checks ("if user exists $\rightarrow$ if authorized $\rightarrow$ if has active plan $\rightarrow$ if payment valid...").

Deeply nested code increases cyclomatic complexity, wastes 2 to 6 tokens per line on leading indentation whitespace, and creates cognitive fatigue during subsequent code reviews and diff patching.

The Compact Guard Clause Protocol (also known as the Bouncer Pattern) checks for failure conditions at the very top of the function and returns/raises immediately, allowing the primary "happy path" to execute unnested at root indentation.


Nested Pyramid vs. Early-Return Guard Clauses

┌─────────────────────────────────────────────────────────────┐
│                 Indentation & Nesting Impact                │
│                                                             │
│  Nested Pyramid of Doom (Deep Nesting / Token Bloat):       │
│  function processOrder(order) {                             │
│    if (order !== null) {                                    │
│      if (order.isValid) {                                   │
│        if (order.items.length > 0) {                        │
│          // 25 lines indented 8 spaces deep...              │
│          return executeCheckout(order);                     │
│        } else { return { err: "Empty items" }; }            │
│      } else { return { err: "Invalid order" }; }            │
│    } else { return { err: "Null order" }; }                 │
│  }                                                          │
│                                                             │
│  Linear Guard Clauses (Bouncer Pattern - 40% Fewer Tokens): │
│  function processOrder(order) {                             │
│    if (!order) return { err: "Null order" };                │
│    if (!order.isValid) return { err: "Invalid order" };     │
│    if (!order.items.length) return { err: "Empty items" };  │
│                                                             │
│    // Happy path executes flat at root level                │
│    return executeCheckout(order);                           │
│  }                                                          │
└─────────────────────────────────────────────────────────────┘

Read the full file on GitHub · 126 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday Changed · -11 tokens per session b1cfb52f51e4
  2. 7d ago First seen · 126 lines · 37 tokens per session scan A e9dc90790b3d

Subscribe to this mod's changes

compact-guard-clauses is a skill published in the GitHub repository alivirgo/Major-AI-Skills (1 stars, last pushed today), licensed MIT. It adds 26 tokens to every session and 1,269 once invoked, about $0.0001 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-09-05.