Refactor for Readability
Improve the structure and naming of code without changing what it does, with a list of changes.
Write clear docstrings and a README section from your code, without inventing behavior.
Document the {language} code below.
<code>
{code}
</code>
<audience>
{audience}
</audience>
<format>
Docstring style: {docstring_style}
README section: {readme_scope}
</format>
Produce two things.
Part 1. The code with docstrings added to every public function, class and module. Each docstring states what it does, its parameters with types, what it returns, and what errors it can raise. Do not change the code itself.
Part 2. A README section covering: {readme_scope}. Include a short usage example that runs with the code as written.
Rules:
- Describe only what the code actually does. Do not invent features, parameters, defaults or configuration that are not in the code.
- Use plain language suitable for the audience. Avoid restating the function name as the description.
- If behavior is unclear from the code, or if names are misleading, list those places under "Needs your input" instead of guessing.
- Keep each docstring short. A one-line summary first, then details only where useful.
End with a list of anything in the documentation you could not confirm from the code alone.| Variable | What to put in | Example |
|---|---|---|
| {language} | The programming language. | Python |
| {code} | The code to document. | Paste the module or function here. |
| {audience} | Who will read the documentation. | New developers joining the team. |
| {docstring_style} | The docstring format your project uses. | Google style |
| {readme_scope} | What the README section should cover. | Purpose, installation, usage example, configuration. |
def total(items, discount=0.0): """Return the order total after applying a discount. Args: items: A list of (price, quantity) pairs. discount: A fraction between 0 and 1. Raises: ValueError: If a quantity is negative. """ Needs your input: should discount above 1 raise an error or be capped?
Claude tends to write accurate docstrings when told not to invent behavior. Keep the "needs your input" list.
Works as-is. Ask for the README in Markdown so you can paste it straight into your repository.
If you can attach the repository files, name them so the README refers to real file names and commands.
Keep documentation tied to the code you provide. Ask it to leave out claims about performance or compatibility that you did not state.
Shorten each docstring to one line plus the arguments.Add a troubleshooting section based on the errors the code can raise.Rewrite the README introduction for a non-technical reader.Improve the structure and naming of code without changing what it does, with a list of changes.
Get a prioritized code review that covers correctness, security, readability and tests.
Generate unit tests that cover normal cases, edge cases and failures, with the reasoning for each.