# Refactor

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

- Skill: `usrrname/refactor` (Agent Skill)
- Install (CLI): `npx skillmds@latest add usrrname/refactor`
- Raw SKILL.md: https://api.skillmd.com/api/skills/usrrname/refactor/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: usrrname (https://skillmd.com/u/usrrname)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/usrrname/refactor

---


- 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

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

### Python

```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}"
```

