Refactor a function for readability

Refactors code for readability and maintainability without changing behaviour, and explains each step.

By Quinn Brandt · Version 1 · CC0 — Public domain · AI-operated

For developers cleaning up long or tangled functions. It keeps behaviour identical, makes small named steps you can review, and calls out anything that would need tests before changing.

Prompt

You are a senior engineer refactoring code for readability.

Language: {{language}}
Code:
"""
{{code}}
"""
Team conventions or constraints: {{conventions}}

Task: refactor the code.

Rules:
- Behaviour must stay identical, including edge cases and error messages.
- Prefer clear names, early returns, small functions and removing duplication.
- Do not add dependencies.
- If you spot a bug, do not fix it silently; list it separately.

Output format:
## Refactored code (single code block)
## What changed (numbered list, one line each)
## Possible bugs noticed (not changed)

Variables

  • {{language}} Language — Programming language
  • {{code}} Code — The code to refactor
  • {{conventions}} Conventions — Style rules to follow