agentleFS
selamy-labsselamy-labs/agent-skills/skills/early-return-over-else/SKILL.md

early-return-over-else

Skill0 starsChanged 5 days agoMITView on GitHub

Use when control flow is getting nested or hard to scan. Prefer guard clauses and early returns while preserving behavior with tests.

The file

We split this skill into the rules it gives an agent

7 rules. About 0 are common, 5 are distinctive, and 2 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈5

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 2

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Use this skill when a function grows nested if/else blocks, mixes validation with the main path, or forces readers to keep too much state …

    line 8
  2. Very specific:

    Remove else blocks that only exist because the earlier branch did not return.

    line 17
  3. Distinctive:

    Keep cleanup, transactions, and resource lifetimes explicit.

    line 19
  4. Distinctive:

    Run the focused tests that cover both the guard path and the main path.

    line 21
  5. Distinctive:

    Every early exit has a clear reason and a behavior test or existing coverage.

    line 26
  6. Distinctive:

    No guard silently swallows an error that should be visible to callers.

    line 27
  7. Distinctive:

    No broad refactor was mixed into a behavior fix.

    line 28

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Early Return Over Else

Use this skill when a function grows nested if/else blocks, mixes validation with the main path, or forces readers to keep too much state in mind.

Workflow

  1. Identify the main success path and the exceptional, invalid, empty, or already-complete cases.
  2. Move each edge case into a guard clause that returns, raises, continues, or breaks before the main path.
  3. Remove else blocks that only exist because the earlier branch did not return.
  4. Keep cleanup, transactions, and resource lifetimes explicit. Do not return early past required finalization unless the language construct guarantees it.
  5. Run the focused tests that cover both the guard path and the main path.

Review Checks

  • The main path is less indented than before.
  • Every early exit has a clear reason and a behavior test or existing coverage.
  • No guard silently swallows an error that should be visible to callers.
  • No broad refactor was mixed into a behavior fix.

Anti-Patterns

  • Converting simple two-branch code into scattered exits with worse locality.
  • Returning a default value when the previous code raised or logged an error.
  • Using early returns to avoid modeling states that should be explicit.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.