Skip to main content
Coding Prompts

Refactor for Readability

Improve code readability through better naming, structure, and clarity without changing behavior.

intermediateWorks with any modelCoding
Prompt
Refactor the following [LANGUAGE] code for readability. Do NOT change its external behavior or public API.

**Style guide to follow (if any):** [CODING_STYLE_GUIDE]

**Code:**
```[LANGUAGE]
[CODE]
```

Apply these improvements where relevant:
1. **Naming** — rename variables, functions, and parameters to be self-documenting
2. **Function extraction** — break complex logic into smaller, well-named helper functions
3. **Type hints / JSDoc** — add or improve type annotations and documentation comments
4. **Redundancy removal** — eliminate dead code, repeated logic, and unnecessary comments
5. **Clarity comments** — add brief inline comments only where the "why" is non-obvious

Present the result as a before/after comparison with a brief note explaining each change made.

How to Use

Paste code that works but is hard to read — dense one-liners, cryptic variable names, functions that do too many things, or code that only its author understands. The refactored version will be functionally identical but significantly easier to maintain and review. Ideal for preparing code for pull requests or onboarding teammates.

Variables

VariableDescription
[LANGUAGE]Programming language (e.g., Python, TypeScript, Java, Go)
[CODE]The working but hard-to-read code you want cleaned up
[CODING_STYLE_GUIDE]Optional: name of a style guide to follow (e.g., PEP 8, Google Java Style, Airbnb JavaScript, Standard Go). Leave blank to use community conventions for the language.

Tips

  • Explicitly say "do not change external behavior" to prevent the model from sneaking in logic changes alongside style improvements — these should be separate commits.
  • If you only want one type of improvement (e.g., just better naming, no function extraction), add that constraint to the prompt to keep the diff focused.