AgentSkillsCN

code-simplifier

在保持全部功能不变的前提下,简化并优化代码,使其更清晰、更一致、更易维护。除非另有指示,否则重点聚焦于近期修改的代码。

SKILL.md
--- frontmatter
name: code-simplifier
description: Simplifies and refines code for clarity, consistency, and maintainability while preserving all functionality. Focuses on recently modified code unless instructed otherwise.

Code Simplifier

Expert code simplification focused on clarity, consistency, and maintainability while preserving exact functionality. Prioritizes readable, explicit code over overly compact solutions.

Core Principles

1. Preserve Functionality

Never change what the code does — only how it does it. All original features, outputs, and behaviors must remain intact.

2. Apply Project Standards

Follow established coding standards from the project's CLAUDE.md or equivalent, including:

  • Proper import sorting and module conventions
  • Preferred function declaration style (e.g., function keyword over arrow functions)
  • Explicit return type annotations for top-level functions
  • Proper component patterns with explicit Props types
  • Proper error handling patterns (avoid try/catch when possible)
  • Consistent naming conventions
  • File naming conventions (e.g., snake_case)

3. Enhance Clarity

  • Reduce unnecessary complexity and nesting
  • Eliminate redundant code and abstractions
  • Use clear, descriptive variable and function names
  • Consolidate related logic
  • Remove comments that describe obvious code
  • Avoid nested ternary operators — prefer switch statements or if/else chains for multiple conditions
  • Choose clarity over brevity — explicit code is better than dense one-liners

4. Maintain Balance — Do Not Over-Simplify

Avoid:

  • Overly clever solutions that are hard to understand
  • Combining too many concerns into single functions or components
  • Removing helpful abstractions that improve organization
  • Prioritizing "fewer lines" over readability (e.g., nested ternaries, dense one-liners)
  • Making code harder to debug or extend

Refinement Process

  1. Identify recently modified code sections (check git diff, session edit history, or open files)
  2. Analyze for opportunities to improve clarity and consistency
  3. Apply project-specific best practices and coding standards
  4. Verify all functionality remains unchanged
  5. Confirm the refined code is simpler and more maintainable

Scope

  • Default: Only refine code that has been recently modified or touched in the current session
  • Expanded: Refine broader scope only when explicitly instructed

Workflow

When invoked (either explicitly or proactively after code changes):

  1. Read the project's CLAUDE.md (or equivalent) for project-specific standards
  2. Identify recently changed files via git status/diff or session context
  3. Read and analyze the changed code
  4. Apply refinements following the core principles above
  5. Run the project's linter (make check-style-fix or equivalent) to verify standards compliance
  6. Run relevant tests to confirm functionality is preserved
  7. Summarize significant changes made

Examples

Reducing Nesting

Before:

go
func process(items []Item) error {
    if len(items) > 0 {
        for _, item := range items {
            if item.IsValid() {
                if err := item.Save(); err != nil {
                    return err
                }
            }
        }
    }
    return nil
}

After:

go
func process(items []Item) error {
    for _, item := range items {
        if !item.IsValid() {
            continue
        }
        if err := item.Save(); err != nil {
            return err
        }
    }
    return nil
}

Avoiding Nested Ternaries

Before:

typescript
const label = status === 'active' ? 'Active' : status === 'pending' ? 'Pending' : status === 'error' ? 'Error' : 'Unknown';

After:

typescript
function getStatusLabel(status: string): string {
    switch (status) {
        case 'active':
            return 'Active';
        case 'pending':
            return 'Pending';
        case 'error':
            return 'Error';
        default:
            return 'Unknown';
    }
}

Consolidating Related Logic

Before:

go
func handleRequest(r *Request) (*Response, error) {
    userID := r.GetUserID()
    if userID == "" {
        return nil, errors.New("missing user ID")
    }

    channelID := r.GetChannelID()
    if channelID == "" {
        return nil, errors.New("missing channel ID")
    }

    teamID := r.GetTeamID()
    if teamID == "" {
        return nil, errors.New("missing team ID")
    }

    // ... rest of handler
}

After:

go
func handleRequest(r *Request) (*Response, error) {
    if err := validateRequiredFields(r); err != nil {
        return nil, err
    }

    // ... rest of handler
}

func validateRequiredFields(r *Request) error {
    required := map[string]string{
        "user ID":    r.GetUserID(),
        "channel ID": r.GetChannelID(),
        "team ID":    r.GetTeamID(),
    }
    for name, value := range required {
        if value == "" {
            return fmt.Errorf("missing %s", name)
        }
    }
    return nil
}