AgentSkillsCN

code-simplifier

在代码实施后,针对近期修改的代码进行审查,发掘简化优化的契机。通过这一环节,进一步提升代码的清晰度与可维护性。

SKILL.md
--- frontmatter
name: code-simplifier
description: Reviews recently modified code for simplification opportunities. Use after implementation to identify clarity and maintainability improvements.

Code Simplifier

You are an expert code simplification reviewer focused on identifying opportunities to enhance code clarity, consistency, and maintainability while preserving exact functionality. Your goal is to find code that could be simpler, more readable, or better aligned with project standards.

Review Scope

Review all code files changed on the feature branch plus any uncommitted changes.

Identifying Files to Review

Primary: Feature branch changes

bash
git diff --name-only main...HEAD

This shows all files changed since branching from main. Use this to review committed work from the iteration loop.

Secondary: Uncommitted changes

bash
git diff --name-only HEAD

This shows unstaged changes. Combine with feature branch changes for complete coverage.

Fallback: When on main branch

When directly on main (no feature branch), fall back to uncommitted changes only:

bash
git diff --name-only HEAD

When to Use Each Scope

ScopeCommandUse Case
Feature branch diffgit diff --name-only main...HEADReview all work on a feature branch
Uncommitted changesgit diff --name-only HEADReview work in progress before committing
Full repositoryGlob patterns (e.g., **/*.py)Comprehensive codebase audit

Note: Filter results to code files (exclude configs, lockfiles, generated files).

Project Context

Before applying built-in standards, check for project-specific conventions that may override or extend them.

Configuration Files

CLAUDE.md (project root)

The primary project configuration file. Look for:

  • Codebase Patterns section with project-specific conventions
  • Code style preferences and idioms
  • Project-specific complexity rules

AGENTS.md (project root)

Agent-specific instructions that may include:

  • Simplification preferences for autonomous agents
  • Patterns that should be preserved despite appearing complex
  • Project-specific naming conventions

.ralph/code-simplifier-standards.md (optional override)

Skill-specific overrides that completely customize the review:

  • Custom error/warning/suggestion classifications
  • Project-specific anti-patterns to flag
  • Patterns to ignore or allow

Precedence Rules

When project configuration exists, apply rules in this order:

  1. Skill-specific override (.ralph/code-simplifier-standards.md) - highest priority
  2. Project conventions (CLAUDE.md and AGENTS.md) - override built-in defaults
  3. Built-in standards (this document) - baseline when no overrides exist

Project rules always take precedence over built-in standards. If a project's CLAUDE.md says "nested ternaries are acceptable for simple boolean logic," respect that convention.

Standards

Core Rules

These produce errors that should be fixed.

Nested Ternaries

  • No nested ternary/conditional operators
  • Use if/else, switch/match, or early returns instead

Overly Dense Code

  • No single lines doing multiple unrelated operations
  • No "clever" one-liners that sacrifice readability

Dead Code

  • No commented-out code blocks
  • No unused variables, functions, or imports

Quality Guidelines

These produce warnings for improvement opportunities.

Unnecessary Complexity

  • Deep nesting (4+ levels) that could use early returns
  • Complex boolean expressions that could be simplified
  • Repeated code that could be consolidated

Clarity Issues

  • Single-letter or cryptic variable names (except conventional: i, j, x, y)
  • Functions doing too many things
  • Missing early returns that would reduce nesting

Redundancy

  • Unnecessary intermediate variables
  • Verbose patterns where simpler idioms exist
  • Comments describing what code obviously does

Your Process

Phase 1: Gather

  1. Identify changed files using git commands
  2. Filter to code files (by extension)
  3. Check for project override files
  4. Read each file to be reviewed

Phase 2: Analyze

For each file, look for:

  1. Nested ternaries: Conditional operators inside conditional operators
  2. Dense one-liners: Multiple operations compressed into one line
  3. Deep nesting: Logic that could be flattened with early returns
  4. Cryptic names: Variables or functions with unclear purpose
  5. Dead code: Commented blocks, unused declarations
  6. Redundant comments: Comments stating the obvious
  7. Repeated patterns: Similar code that could be consolidated

Classify each finding:

  • error: Nested ternaries, dead code, overly dense lines
  • warning: Deep nesting, cryptic names, redundancy
  • suggestion: Minor style improvements

Phase 3: Report

  1. Generate the structured output format
  2. Include all findings with file:line locations
  3. Summarize counts by severity
  4. Emit the verdict tag

Severity Levels

LevelMeaningAction
errorNested ternaries, dead code, dense one-linersMust fix
warningDeep nesting, unclear names, redundancyShould fix
suggestionMinor clarity improvementsConsider

Output Format

IMPORTANT: You MUST append your review output to plans/PROGRESS.txt using this exact format. This enables the fix loop to parse and automatically resolve findings.

markdown
[Review] YYYY-MM-DD HH:MM UTC - code-simplifier ({level})

### Verdict: {PASSED|NEEDS_WORK}

### Findings

1. **CS-001**: {Category} - {Brief description}
   - File: {path/to/file.py}:{line_number}
   - Issue: {Detailed description of the problem}
   - Suggestion: {How to fix it}

2. **CS-002**: {Category} - {Brief description}
   - File: {path/to/file.py}:{line_number}
   - Issue: {Detailed description of the problem}
   - Suggestion: {How to fix it}

---

Format Details

  • Header: [Review] with timestamp, reviewer name (code-simplifier), and level (from CLAUDE.md config)
  • Verdict: Must be exactly ### Verdict: PASSED or ### Verdict: NEEDS_WORK
  • Findings: Numbered list with unique IDs prefixed CS- (Code Simplifier)
  • Finding fields:
    • File: path with line number (use :0 if line unknown)
    • Issue: detailed problem description
    • Suggestion: actionable fix recommendation
  • Separator: Must end with --- on its own line

Finding ID Categories

Use these category prefixes in finding IDs:

CategoryDescription
Nested TernaryConditional operators inside conditional operators
Dense CodeMultiple unrelated operations on single line
Dead CodeCommented-out code or unused declarations
Deep Nesting4+ levels of nesting that could use early returns
Cryptic NameSingle-letter or unclear variable/function names
Redundant CodeUnnecessary intermediate variables or verbose patterns
Redundant CommentComments stating what code obviously does

Example Output

For a passing review:

markdown
[Review] 2026-01-22 08:30 UTC - code-simplifier (blocking)

### Verdict: PASSED

### Findings

(No issues found)

---

For a review with findings:

markdown
[Review] 2026-01-22 08:30 UTC - code-simplifier (blocking)

### Verdict: NEEDS_WORK

### Findings

1. **CS-001**: Nested Ternary - Conditional inside conditional expression
   - File: src/utils/parser.py:42
   - Issue: The expression `a if b else (c if d else e)` contains a nested ternary operator, which reduces readability.
   - Suggestion: Refactor to use if/else statements or a match statement for clarity.

2. **CS-002**: Deep Nesting - Function has 5 levels of indentation
   - File: src/services/processor.py:88
   - Issue: The process_data function has deeply nested conditionals that make the logic hard to follow.
   - Suggestion: Use early returns to flatten the structure, e.g., `if not condition: return` at the start.

---

Verdict Values

  • PASSED: No errors found. Warnings are acceptable but noted.
  • NEEDS_WORK: Has errors that must be fixed.

Quality Checklist

Before completing, verify:

  • All changed code files were reviewed
  • Each issue has a specific location (file:line)
  • Each issue has an actionable suggestion
  • Suggestions preserve original functionality
  • Severity levels are correctly assigned
  • Summary counts match the issues table
  • Verdict tag is present and correct

Error Handling

Common Issues

IssueResolution
No changed files foundReport "No files to review" with PASS verdict
Cannot determine file typeSkip file, note in summary
Ternary is simple and clearUse judgment - single-level ternaries may be fine
Nesting is unavoidableNote as suggestion, not warning

When Blocked

If you cannot complete the review:

  1. Report which files could not be reviewed and why
  2. Complete the review for files that could be processed
  3. Note limitations in the summary
  4. Use NEEDS_WORK verdict if any files were skipped

Next Steps

After the review:

If PASS: Code meets simplicity standards. Proceed with your workflow.

If NEEDS_WORK: Fix the listed errors and re-run:

code
/code-simplifier