AgentSkillsCN

code-quality-enhancement

以系统化的方法提升代码的质量、可读性和可维护性。当您被要求对现有代码进行重构、清理,或优化改进时,可使用此技能。

SKILL.md
--- frontmatter
name: code-quality-enhancement
description: Systematic approach to improving code quality, readability, and maintainability. Use this when asked to refactor, clean up, or improve existing code.
license: MIT

Code Quality Enhancement Skill

This skill provides a structured methodology for improving code quality, readability, and maintainability in existing codebases.

When to Use This Skill

  • When asked to refactor existing code
  • When improving code readability
  • When reducing code complexity
  • When enhancing maintainability
  • When modernizing legacy code
  • When applying best practices to existing code

Quality Assessment Framework

1. Readability

Assess:

  • Variable and function naming clarity
  • Code organization and structure
  • Comment quality and necessity
  • Code length and complexity

Improve:

  • Use descriptive, intention-revealing names
  • Break down long functions into smaller, focused ones
  • Remove redundant or obvious comments
  • Add meaningful comments for complex logic

2. Maintainability

Assess:

  • Code duplication (DRY principle)
  • Coupling between modules
  • Dependency management
  • Configuration handling

Improve:

  • Extract repeated code into reusable functions
  • Apply dependency injection where appropriate
  • Use configuration files for environment-specific values
  • Implement proper error handling patterns

3. Code Smells

Common issues to identify:

  • Long methods or classes
  • Large parameter lists
  • Primitive obsession
  • Feature envy
  • Data clumps
  • Shotgun surgery

Remediation strategies:

  • Extract method/class
  • Introduce parameter object
  • Replace primitives with domain objects
  • Move behavior closer to data
  • Consolidate related changes

4. Performance

Look for:

  • Inefficient algorithms (O(n²) vs O(n log n))
  • Unnecessary computations in loops
  • Memory leaks or excessive allocation
  • Missing caching opportunities
  • N+1 query problems

Optimize:

  • Use appropriate data structures
  • Move invariant computations out of loops
  • Implement memoization for expensive operations
  • Add caching layers where beneficial
  • Batch database queries

Enhancement Process

Step 1: Analyze Current State

Use available tools to understand the code:

bash
# Find similar code patterns
grep -r "pattern" --include="*.ext"

# Identify complex files
find . -name "*.ext" -exec wc -l {} \; | sort -nr

# Check for code duplication
# Use language-specific tools (jscpd, copy-paste-detector, etc.)

Step 2: Prioritize Improvements

Focus on:

  1. High-impact, low-effort changes first
  2. Critical paths and frequently used code
  3. Areas with known bugs or maintenance issues
  4. Code that violates team standards

Step 3: Apply Improvements Incrementally

  • Make one type of improvement at a time
  • Keep changes small and focused
  • Ensure tests pass after each change
  • Commit related changes together

Step 4: Validate Changes

  • Run existing test suite
  • Check for performance regressions
  • Verify behavior hasn't changed
  • Get code review feedback

Best Practices by Language

JavaScript/TypeScript

  • Use const/let instead of var
  • Apply arrow functions where appropriate
  • Leverage destructuring for cleaner code
  • Use template literals for string concatenation
  • Implement proper async/await patterns

Python

  • Follow PEP 8 style guidelines
  • Use list comprehensions for simple operations
  • Apply context managers (with statements)
  • Use type hints for better documentation
  • Leverage dataclasses for data containers

Java

  • Use modern Java features (streams, Optional)
  • Apply appropriate design patterns
  • Implement proper exception handling
  • Use builder pattern for complex objects
  • Leverage lombok for boilerplate reduction

Go

  • Follow Go idioms and conventions
  • Use defer for cleanup operations
  • Implement proper error handling
  • Use interfaces for abstraction
  • Apply goroutines and channels appropriately

Refactoring Techniques

Extract Method

Break down large functions into smaller, focused ones:

code
Before: One 100-line function doing multiple things
After: One coordinating function + 5 focused helper functions

Extract Class

Separate concerns into distinct classes:

code
Before: One class with 20 methods and 15 fields
After: 3 specialized classes with clear responsibilities

Introduce Parameter Object

Group related parameters:

code
Before: function(name, age, email, phone, address, city, zip)
After: function(userInfo)

Replace Conditional with Polymorphism

Use object-oriented patterns instead of complex conditionals:

code
Before: if/else chain based on type
After: Strategy pattern with type-specific implementations

Code Review Checklist

Before finalizing improvements:

  • Code is more readable than before
  • Complexity is reduced
  • No functionality is broken
  • Tests still pass (or new tests added)
  • Performance is same or better
  • No new security vulnerabilities introduced
  • Code follows project conventions
  • Documentation is updated if needed

Tools and Commands

Linting

bash
# JavaScript/TypeScript
npm run lint
eslint --fix .

# Python
pylint src/
black src/

# Go
golint ./...
go fmt ./...

Testing

bash
# Run tests to ensure no regression
npm test
pytest
go test ./...

Analysis

bash
# Find TODO/FIXME comments
grep -r "TODO\|FIXME" src/

# Check cyclomatic complexity
# Use language-specific tools

Example Workflow

  1. Identify target code for improvement
  2. Understand current behavior - read code and tests
  3. Run existing tests to establish baseline
  4. Make small, focused improvements one at a time
  5. Test after each change to catch regressions early
  6. Commit incrementally with descriptive messages
  7. Get feedback through code review
  8. Document changes in commit messages and code comments

Anti-Patterns to Avoid

  • Don't refactor and add features simultaneously
  • Don't change too much at once
  • Don't skip running tests
  • Don't refactor without understanding the code first
  • Don't optimize prematurely
  • Don't break existing APIs without good reason

Remember: The goal is to make code better, not different. Every change should have a clear purpose and measurable benefit.