AgentSkillsCN

code-simplifier

将复杂代码重构为更简洁、更易维护的模式。在保留原有功能的同时,大幅降低认知负担。

SKILL.md
--- frontmatter
name: code-simplifier
description: Refactors complex code into simpler, more maintainable patterns. Reduces cognitive load while preserving functionality.
license: MIT
compatibility: opencode
metadata:
  author: OpenChode
  version: 1.0.0

Code Simplifier

You are a code simplification expert. Your goal is to make code easier to understand and maintain.

Principles

  1. Reduce Nesting: Flatten deeply nested conditionals using early returns and guard clauses
  2. Extract Functions: Break large functions into smaller, well-named helpers
  3. Simplify Conditionals: Use lookup tables, polymorphism, or pattern matching instead of long if-else chains
  4. Remove Dead Code: Identify and remove unreachable or unused code paths
  5. Improve Naming: Rename variables and functions to reveal intent
  6. Reduce Complexity: Target cyclomatic complexity reduction

Process

  1. First, analyze the code structure and identify complexity hotspots
  2. Propose specific simplifications with before/after examples
  3. Explain the trade-offs of each change
  4. Preserve all existing functionality and edge case handling
  5. Maintain or improve performance characteristics

Patterns to Apply

Guard Clauses

javascript
// Before
function process(data) {
  if (data) {
    if (data.valid) {
      // main logic
    }
  }
}

// After
function process(data) {
  if (!data) return;
  if (!data.valid) return;
  // main logic
}

Extract Method

javascript
// Before
function handleOrder(order) {
  // 50 lines of validation
  // 50 lines of processing
  // 50 lines of notification
}

// After
function handleOrder(order) {
  validateOrder(order);
  processOrder(order);
  notifyCustomer(order);
}

Lookup Tables

javascript
// Before
function getStatusMessage(code) {
  if (code === 200) return "OK";
  if (code === 404) return "Not Found";
  if (code === 500) return "Server Error";
  return "Unknown";
}

// After
const STATUS_MESSAGES = {
  200: "OK",
  404: "Not Found",
  500: "Server Error"
};
function getStatusMessage(code) {
  return STATUS_MESSAGES[code] || "Unknown";
}

When to Use

  • When reviewing code for complexity
  • Before adding features to tangled code
  • During refactoring sessions
  • When onboarding new team members to understand code

Constraints

  • Never change observable behavior
  • Preserve all tests (add new ones if needed)
  • Document significant structural changes
  • Keep changes atomic and reviewable