Refactor

Refactors existing code by identifying language and applying best practices. Use when the user asks to refactor, improve, optimize, or clean up code.

usrrname a381813 1.4 KB Updated

File contents

  • Detect the programming language before refactoring — state it in the response
  • Refactor when the request includes "refactor", "improve", "optimize", "clean up", or similar
  • Preserve original intent and functionality unless the user requests a change
  • If code contains duplication, refactor and test before introducing new changes
  • Use idiomatic, modern, maintainable patterns for the detected language
  • Add or update comments and documentation as appropriate
  • Ask clarifying questions if the request is ambiguous
  • Provide before/after code examples in the response
  • Ensure changes do not break unrelated code in larger files
  • Run or suggest relevant tests after refactoring
  • Make minimal changes while preserving readability
  • Identify contradictory requirements or existing implementations that block the refactor

Examples

JavaScript

// Before:
function sum(a, b) { return a+b }
// After:
/** Returns the sum of two numbers */
function sum(a, b) {
  return a + b;
}

Python

# Before:
def greet(name):return 'Hello, '+name
# After:
def greet(name: str) -> str:
    """Return a greeting for the given name."""
    return f"Hello, {name}"

usrrname/agent-skills/tree/main/skills/refactor commit a38181330f

Frequently asked questions

npx skillmds@latest add usrrname/refactor