Home/Templates/Code Comment Generator
Coding

Code Comment Generator Prompt Template

Generate clear, useful inline comments and JSDoc/docstring documentation for any function or class.

The Prompt

ROLE: You are a senior engineer and documentation advocate who believes that comments are a form of respect for future developers — including your future self at 2am when something is broken in production. CONTEXT: You are adding inline comments and formal documentation (JSDoc, docstrings, or XML docs) to existing code. Good comments are not a translation of what the code does — that's what the code is for. Good comments explain why the code does it that way, what non-obvious assumptions are being made, and what would happen if this code were changed carelessly. TASK: Add professional inline comments and formal documentation to the code below. RULES: • Inline comments must explain WHY, not WHAT — "increment i" is not a comment, "we skip the first element because it's always the header row" is • Every function/method must have a complete [JSDOC/DOCSTRING/XML] block with: purpose, all parameters with types and descriptions, return type and description, thrown exceptions, and an example • Complex logic blocks (loops, conditionals, algorithms) must have a one-line comment before them explaining the intent • Do NOT comment every line — only lines where the purpose is non-obvious • If you find code that appears wrong or dangerous while adding comments, add a [REVI

🔒

Upgrade to see the full prompt

Get unlimited access to all 215+ templates — starting at $9/month.

✦ Upgrade to Pro

Already have an account? Sign in

How to use this template

1

Copy the template

Click the copy button to grab the full prompt text.

2

Fill in the placeholders

Replace anything in [BRACKETS] with your specific details.

3

Paste into any AI tool

Works with ChatGPT, Claude, Gemini, Cursor, and more.

4

Or enhance with AI

Sign in to PromptIt and let AI tailor the prompt to your exact situation in seconds.

Why this prompt works

The WHY not WHAT rule is the most important distinction in commenting practice — AI defaults to explaining what code does because that's easy to derive from the syntax. Forcing WHY commentary requires the AI to reason about intent, which produces far more valuable documentation.

Tips for best results

  • Add 'flag any code that has no tests with [UNTESTED:]' to the RULES — it turns documentation into a test coverage audit simultaneously
  • For legacy codebases, run this on the most confusing file first — the [REVIEW:] flags often surface genuine bugs or design issues that have been living there unnoticed
  • After generating, remove any comment that just restates the code in English ('// loop over array' above a forEach call) — these add noise, not signal
  • Ask the AI to generate the documentation separately from the code so you can diff what was added — easier to review than a fully mixed file

More Coding templates

Code Review

Get a comprehensive AI code review covering bugs, performance issues, security vulnerabilities, best practice violations, and refactoring opportunities with specific line references.

View →

Debug an Error

Diagnose any code error with a structured breakdown: root cause analysis, step-by-step fix, and prevention strategies for the future.

View →

Explain Code Simply

Translate complex code into plain English with line-by-line explanations, real-world analogies, and edge-case analysis for any skill level.

View →
← Browse all 215 templates