Code Comment Prompt for Useful Comments
A structured prompt that adds clear, non-obvious comments and docstrings to your code — explaining why, not just what — with a real example.
Enhanced prompt — ready to use
ROLE You are a senior software developer with expertise in code documentation and best commenting practices. Your goal is to enhance the clarity and maintainability of source code through comprehensive, well-structured comments. TASK Add clear, concise, and informative comments to the provided code. Think through this step by step, explaining the purpose of functions, logic behind complex statements, and any assumptions made within the code. CONTEXT The user has a piece of source code (unspecified language) that requires improved readability and comprehensibility through comments. The audience is other developers who may read or maintain this code in the future. REQUIREMENTS - Include high-level comments describing overall code functionality and purpose. - Add inline comments for complex logic or important operations. - Follow best practices for code commenting relevant to the programming language involved. - Think step by step to ensure all critical code sections are documented. - Before finalizing, verify that comments are accurate, clear, and complete. - Assumptions: In absence of specific programming language or code snippet, assume a general-purpose language like Python or JavaScript for commenting style. OUTPUT FORMAT Provide the fully commented code with added comments integrated appropriately either above functions/methods or inline, preserving the original code structure.
That's what PromptAI produced from this vague prompt: “add comments to my code”
Example result
```python
# Function to calculate the factorial of a given non-negative integer n.
# The factorial of n (denoted as n!) is the product of all positive integers up to n.
# For example, factorial(5) = 5 * 4 * 3 * 2 * 1 = 120.
# This implementation uses a recursive approach.
def factorial(n):
# Base case: factorial of 0 or 1 is 1 by definition.
if n == 0 or n == 1:
return 1
# Recursive case: factorial(n) = n * factorial(n-1)
return n * factorial(n - 1)
# Function to compute the nth Fibonacci number.
# The Fibonacci sequence starts with 0 and 1, and each subsequent number
# is the sum of the previous two.
# For example, fibonacci(5) returns 5 (sequence: 0,1,1,2,3,5,...).
def fibonacci(n):
# Base case: fibonacci(0) = 0
if n == 0:
return 0
# Base case: fibonacci(1) = 1
elif n == 1:
return 1
# Recursive case: fibonacci(n) = fibonacci(n-1) + fibonacci(n-2)
return fibonacci(n - 1) + fibonacci(n - 2)
# Function to check if a given string is a palindrome.
# A palindrome is a string that reads the same forwards and backwards,
# ignoring case and non-alphanumeric characters.
def is_palindrome(s):
# Normalize the string by removing non-alphanumeric characters and converting to lowercase
filtered_chars = [char.lower() for char in s if char.isalnum()]
# Check if the normalized string is equal to its reverse
return filtered_chars == filtered_chars[::-1]
# Function to find the maximum element in a list of integers.
# Returns None if the list is empty.
def find_max(numbers):
if not numbers:
# Return None to indicate no elements to compare
return None
max_value = numbers[0] # Initialize max_value to first element
# Iterate through each number to find the maximum
for num in numbers[1:]:
if num > max_value:
max_value = num # Update max_value if current num is greater
return max_value
# Example usage demonstrating the defined functions
if __name__ == "__main__":
print("Factorial of 5:", factorial(5)) # Expected output: 120
print("Fibonacci number at position 7:", fibonacci(7)) # Expected output: 13
print("Is 'Racecar' a palindrome?", is_palindrome("Racecar")) # Expected output: True
print("Maximum in [3, 5, 2, 8, 1]:", find_max([3, 5, 2, 8, 1])) # Expected output: 8
```How to adapt it
- •Paste the code and your language so docstring style matches conventions.
- •Ask it to comment the why and tricky parts, not every trivial line.
Have your own rough prompt? Enhance it into a structured prompt like this in one click.
Enhance your own promptMore coding prompts
API Documentation Prompt for Clear Docs
A structured prompt that documents your API endpoint — params, responses, errors, and examples — in clean reference style, with a real example.
Bash Script Prompt for Shell Automation
A structured prompt that writes a safe, portable Bash script for your task — with checks, comments, and a real example output.
Bug Fix Prompt That Finds Root Causes
A structured prompt that diagnoses and fixes a bug — root cause, the fix, and why it works — instead of a guess, with a real example.
Code Review Prompt for Better Feedback
A structured prompt that turns ChatGPT into a thorough code reviewer — bugs, edge cases, readability, and security — with a real example review.
Database Schema Prompt for Clean Data Models
A structured prompt that designs a normalized database schema for your app — tables, keys, and relations — with DDL and a real example.
Dockerfile Prompt for Production Images
A structured prompt that writes a secure, optimized Dockerfile for your app — multi-stage, small, and cache-friendly — with a real example.