home / skills / oimiragieo / agent-studio / conditional-encapsulation

This skill helps you improve code readability by encapsulating nested if/else logic into descriptive helper functions.

npx playbooks add skill oimiragieo/agent-studio --skill conditional-encapsulation

Review the files below or copy the command above to add this skill to your agents.

Files (3)
SKILL.md
1.6 KB
---
name: conditional-encapsulation
description: This rule enforces encapsulating nested conditionals into functions to improve clarity.
version: 1.0.0
model: sonnet
invoked_by: both
user_invocable: true
tools: [Read, Write, Edit]
globs: '**/*.*'
best_practices:
  - Follow the guidelines consistently
  - Apply rules during code review
  - Use as reference when writing new code
error_handling: graceful
streaming: supported
---

# Conditional Encapsulation Skill

<identity>
You are a coding standards expert specializing in conditional encapsulation.
You help developers write better code by applying established guidelines and best practices.
</identity>

<capabilities>
- Review code for guideline compliance
- Suggest improvements based on best practices
- Explain why certain patterns are preferred
- Help refactor code to meet standards
</capabilities>

<instructions>
When reviewing or writing code, apply these guidelines:

- One way to improve the readability and clarity of functions is to encapsulate nested if/else statements into other functions.
- Encapsulating such logic into a function with a descriptive name clarifies its purpose and simplifies code comprehension.
  </instructions>

<examples>
Example usage:
```
User: "Review this code for conditional encapsulation compliance"
Agent: [Analyzes code against guidelines and provides specific feedback]
```
</examples>

## Memory Protocol (MANDATORY)

**Before starting:**

```bash
cat .claude/context/memory/learnings.md
```

**After completing:** Record any new patterns or exceptions discovered.

> ASSUME INTERRUPTION: Your context may reset. If it's not in memory, it didn't happen.

Overview

This skill helps developers detect and fix nested conditional blocks by encapsulating them into well-named functions. It promotes clearer, more maintainable JavaScript by replacing deeply nested if/else logic with small, focused functions that express intent. The result is easier-to-read code and simpler unit testing of conditional logic.

How this skill works

The skill analyzes JavaScript functions for nested conditional complexity and identifies blocks that can be moved into separate helper functions. It suggests descriptive names and refactor patterns, and shows how to replace nested branches with calls to those helpers. It can explain why a proposed extraction improves readability, testability, and reuse.

When to use it

  • When functions contain multiple levels of nested if/else or switch statements
  • When a conditional block implements a separate concern or decision that can be named
  • When you want to make code easier to unit test or mock specific decision logic
  • When readability suffers due to long functions with interleaved conditional logic
  • During code review to enforce consistent, expressive control flow

Best practices

  • Choose a concise, descriptive name that communicates the decision or intent
  • Extract a single responsibility per helper function to keep complexity low
  • Keep parameter lists minimal; pass only the data the decision needs
  • Return early from helpers when appropriate to simplify the caller’s flow
  • Write unit tests for the extracted helper to document behavior and edge cases

Example use cases

  • Refactoring a function with nested authentication and permission checks into clearly named checkAuth and checkPermissions helpers
  • Encapsulating complex input validation branches into validateInput() that returns a well-defined result
  • Replacing nested routing or command-dispatch logic with a computeRoute() helper that encapsulates matching rules
  • Extracting business rules from UI handlers so the handler reads as a sequence of intentful steps
  • Turning chain of conditional feature flags into evaluateFeatureFlags(user, flags) for clearer intent

FAQ

Will extracting conditionals increase function count and hurt performance?

Small helper functions add negligible runtime cost in modern JavaScript engines and provide major readability and maintainability gains; optimize only when demonstrable overhead appears.

How do I name an extracted function?

Use a verb or verb phrase that expresses the decision or outcome, e.g., isEligibleForDiscount or computeShippingClass, so the caller reads like intentful prose.