Code Comment Generator Prompt
Add useful comments and documentation to existing code while preserving behavior, matching project conventions, and removing narration that merely repeats syntax.
Prompt Template
You are a senior developer improving comments and documentation in existing code without changing its behavior. Code to document: [paste complete relevant code] Language and version: [details] Framework or runtime: [details] Audience: [maintainers, library users, beginners, operators] Project comment conventions: [paste examples or style guide] Documentation format: [docstrings, JSDoc, XML comments, inline comments, block comments] Public API expectations: [parameters, return values, errors, side effects] Domain terms that need explanation: [list] Non-obvious constraints or historical decisions: [verified context] Sections that must remain untouched: [list] Generated files or vendor code: [identify and exclude] Desired comment density: [minimal, standard, teaching-oriented] Language for comments: [language] Lint or documentation tooling: [details] Provide: 1. A brief comment audit identifying public interfaces, complex logic, invariants, side effects, and misleading or stale comments. 2. The complete annotated code with behavior, identifiers, formatting, and control flow unchanged. 3. Public documentation that accurately covers parameters, return values, exceptions or errors, side effects, and important preconditions. 4. Inline comments only where they explain why, an invariant, a boundary case, a workaround, or a non-obvious algorithm step. 5. Removal or rewriting of comments that merely restate syntax, contradict the code, or claim unverified behavior. 6. A change summary listing comments added, revised, and removed. 7. A list of missing context that prevented a confident comment, using TODO wording only when I approve it. 8. A final check that no executable token or behavior changed. Do not invent business rules, performance guarantees, issue numbers, authors, dates, package behavior, or test results. If the code and supplied explanation disagree, flag the conflict instead of documenting a guess.
Example Output
Comment Audit
- Add a public docstring describing the accepted bounds and return value.
- Explain why lower-bound comparison runs before upper-bound comparison.
- Remove the comment that repeats the final return statement.
Annotated Code
def clamp(value: float, lower: float, upper: float) -> float:
"""Return `value` constrained to the inclusive interval [`lower`, `upper`].
Raises:
ValueError: If `lower` is greater than `upper`.
"""
if lower > upper:
raise ValueError("lower must not exceed upper")
# Check boundaries explicitly so values equal to either bound remain unchanged.
if value < lower:
return lower
if value > upper:
return upper
return value
Behavior Check
Executable tokens and control flow are unchanged; only the docstring and one explanatory comment were added.
Tips for Best Results
- ๐กPaste complete surrounding functions so comments describe real control flow rather than an isolated snippet.
- ๐กProvide the project's documentation convention to avoid mixing incompatible comment styles.
- ๐กAsk the model to preserve executable tokens and report any missing context instead of inventing intent.
Frequently Asked Questions
What is the Code Comment Generator Prompt prompt?
Add useful comments and documentation to existing code while preserving behavior, matching project conventions, and removing narration that merely repeats syntax. It's a free ChatGPT prompt template from our Coding collection โ copy it, fill in the bracketed variables, and paste it into your AI tool.
Which AI tools work with this prompt?
It's written and tested for ChatGPT, Claude and Gemini. Any AI assistant that accepts free-form text prompts will handle it well.
How do I customize this ChatGPT prompt?
Replace the bracketed variables โ such as [paste complete relevant code], [details], [paste examples or style guide] โ with your own details before running it. Paste complete surrounding functions so comments describe real control flow rather than an isolated snippet.
Is this prompt free to use?
Yes. Every prompt on PromptAtlas is free to copy, customize, and use โ no signup required.
Related Prompts
Python FileNotFoundError Debugging Prompt
Debug Python FileNotFoundError from the exact traceback, resolved path, working directory, filesystem context, deployment environment, and failing operation.
Python ZeroDivisionError Debugging Prompt
Debug Python ZeroDivisionError from the exact traceback, operand values and types, data path, numeric semantics, runtime context, and failing input.
Python IndentationError Debugging Prompt
Debug Python IndentationError from the exact parser message, whitespace, block structure, source bytes, formatter settings, and runtime version.
Python SyntaxError: Invalid Syntax Debugging Prompt
Debug Python SyntaxError invalid syntax from the exact parser location, surrounding tokens, Python version, generated code, execution context, and minimal reproduction.
Python AttributeError: Object Has No Attribute Debugging Prompt
Debug Python AttributeError from the exact traceback, runtime object type, attribute contract, construction path, version, imports, and minimal reproduction.
Python UnboundLocalError Debugging Prompt
Debug Python UnboundLocalError from the exact traceback, binding scope, assignments, branches, closures, imports, runtime path, and minimal reproduction.