code-simplifier

📁 keep-starknet-strange/starkzap 📅 2 days ago
3
总安装量
3
周安装量
#57548
全站排名
安装命令
npx skills add https://github.com/keep-starknet-strange/starkzap --skill code-simplifier

Agent 安装分布

opencode 3
gemini-cli 3
github-copilot 3
codex 3
kiro-cli 3
kimi-cli 3

Skill 文档

Code Simplifier

Expert code simplification specialist focused on enhancing code clarity, consistency, and maintainability while preserving exact functionality.

When to Use

Invoke this skill when:

  • Code has been recently modified and needs refinement
  • User asks to “simplify”, “clean up”, or “refactor” code
  • Code review reveals complexity or inconsistency
  • After implementing features to ensure code quality
  • User asks to make code more readable or maintainable

Examples:

  • “Simplify this code”
  • “Clean up the recent changes”
  • “Make this more readable”
  • “Refactor for clarity”
  • “Apply coding standards to this file”

Core Principles

You are an expert software engineer with years of experience mastering the balance between readable, explicit code and overly compact solutions.

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 CLAUDE.md or project conventions:

  • ES modules: Proper import sorting and extensions
  • Functions: Prefer function keyword over arrow functions
  • Type annotations: Explicit return types for top-level functions
  • React patterns: Explicit Props types for components
  • Error handling: Proper patterns (avoid unnecessary try/catch)
  • Naming: Consistent conventions throughout

3. Enhance Clarity

Simplify code structure by:

  • ✓ Reducing unnecessary complexity and nesting
  • ✓ Eliminating redundant code and abstractions
  • ✓ Improving readability through clear variable and function names
  • ✓ Consolidating related logic
  • ✓ Removing unnecessary comments that describe obvious code
  • ✓ CRITICAL: Avoid nested ternary operators – prefer switch statements or if/else chains
  • ✓ Choose clarity over brevity – explicit code is often better than compact code

4. Maintain Balance

Avoid over-simplification that could:

  • ✗ Reduce code clarity or maintainability
  • ✗ Create overly clever solutions that are hard to understand
  • ✗ Combine too many concerns into single functions
  • ✗ Remove helpful abstractions that improve organization
  • ✗ Prioritize “fewer lines” over readability (nested ternaries, dense one-liners)
  • ✗ Make code harder to debug or extend

5. Focus Scope

Default behavior: Only refine code that has been recently modified or touched in the current session.

Explicit scope: Follow user instructions if they specify broader or narrower scope.

Instructions

Step 1: Identify Recently Modified Code

Determine what code to analyze:

If not specified by user, find recent changes:

# Check git status for modified files
git status --short

# See recent changes
git diff --name-only HEAD~1

Read project CLAUDE.md for standards:

# Look for coding standards
cat CLAUDE.md
# Or search for standards in project root
ls -la | grep -i "contributing\|standards\|style"

Step 2: Analyze Code for Improvements

For each file in scope:

  1. Read the file to understand current implementation

  2. Identify opportunities for simplification:

    • Complex nested logic
    • Redundant code patterns
    • Unclear variable/function names
    • Violation of project standards
    • Nested ternaries or hard-to-read expressions
    • Unnecessary abstractions
  3. Plan refinements that preserve functionality

Step 3: Apply Refinements

Make targeted improvements:

Use Edit tool for surgical changes that:

  • Simplify complex expressions
  • Rename for clarity
  • Consolidate redundant logic
  • Apply project standards
  • Improve code structure

Common patterns to fix:

// BEFORE: Nested ternary (hard to read)
const result = condition1 ? value1 : condition2 ? value2 : value3;

// AFTER: Switch or if/else (clear)
let result;
if (condition1) {
  result = value1;
} else if (condition2) {
  result = value2;
} else {
  result = value3;
}
// BEFORE: Arrow function at top level
const processData = (data) => {
  return data.map(item => item.value);
}

// AFTER: function keyword with explicit return type
function processData(data: Data[]): number[] {
  return data.map(item => item.value);
}
// BEFORE: Unclear variable names
const d = new Date();
const x = d.getTime();

// AFTER: Clear names
const currentDate = new Date();
const timestamp = currentDate.getTime();

Step 4: Verify Functionality Preserved

After making changes:

  1. Run tests if they exist:
npm test
# or
pytest
# or project-specific test command
  1. Check types for TypeScript/typed projects:
tsc --noEmit
# or
npx tsc --noEmit
  1. Verify build still works:
npm run build
# or project-specific build command

Step 5: Document Significant Changes

Only document changes that affect understanding:

List the refinements made:

  • What was simplified
  • Why (if not obvious)
  • Any trade-offs considered

Example summary:

Simplified the authentication flow:
- Replaced nested ternaries with switch statement for clarity
- Renamed `x` → `authToken` for readability
- Consolidated duplicate validation logic
- Applied project standard: function keyword over arrows

Functionality preserved: All tests pass ✓

Autonomous Operation

Important: This skill operates proactively. When invoked:

  1. Don’t ask for permission to make improvements
  2. Apply refinements immediately based on the principles above
  3. Ensure all changes preserve functionality
  4. Present summary of changes made

Examples

Example 1: Simplify Recent Changes

User: "/code-simplifier"

Actions:
1. Check git status for modified files
2. Read CLAUDE.md for project standards
3. Analyze modified files for clarity improvements
4. Apply refinements (nested ternaries → switch, unclear names → clear names)
5. Run tests to verify functionality
6. Present summary of improvements

Example 2: Specific File Scope

User: "Simplify src/auth/login.ts"

Actions:
1. Read src/auth/login.ts
2. Check for project standards
3. Identify improvement opportunities
4. Apply refinements
5. Verify with tests
6. Document changes

Example 3: Broader Scope

User: "Clean up all code in src/components/"

Actions:
1. Use Glob to find all files in src/components/
2. Read each file
3. Apply simplification principles
4. Make targeted edits
5. Run component tests
6. Summarize all changes

Common Simplification Patterns

Pattern Before After
Nested ternary a ? b : c ? d : e Switch or if/else chain
Unclear names x, data, tmp Descriptive names
Redundant logic Same check repeated Extract to function/variable
Arrow at top-level const fn = () => {} function fn() {}
Missing types function fn(x) function fn(x: Type): ReturnType
Deep nesting 4+ levels of indentation Early returns, guard clauses
Magic numbers if (status === 200) if (status === HTTP_OK)

Anti-Patterns to Avoid

Don’t sacrifice readability for brevity:

// TOO COMPACT (avoid)
const r = d.map(x=>x.v).filter(x=>x>0).reduce((a,b)=>a+b,0);

// CLEAR AND READABLE (prefer)
const values = data.map(item => item.value);
const positiveValues = values.filter(value => value > 0);
const sum = positiveValues.reduce((total, current) => total + current, 0);

Don’t remove helpful abstractions:

// GOOD: Clear abstraction
function isValidEmail(email: string): boolean {
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  return emailRegex.test(email);
}

// BAD: Inline makes it harder to understand
if (/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) { ... }

Success Criteria

✓ All original functionality preserved ✓ Code is more readable and maintainable ✓ Project standards applied consistently ✓ Tests pass (if applicable) ✓ No clever tricks that obscure intent ✓ Clear > compact ✓ Future developers can understand and extend the code easily