Prompts

Claude Prompts for Coding: Debug, Refactor, Test

Claude handles most common coding tasks well — debugging, refactoring, writing tests, code review, documentation, and explaining unfamiliar code. This page collects copy-ready prompt templates for each task, organized by use case.

See our Claude for Coding guide for a broader overview of how to use Claude in development workflows, and our Claude Prompts hub for general prompting tips.

Debugging Prompts

Basic Debugging

Here is an error I'm getting:
[paste error message or stack trace]

Here is the relevant code:
[paste code]

What is causing this error and how do I fix it?

Unexpected Behavior (No Error)

This code runs without errors but produces unexpected output. Expected: [what should happen]. Actual: [what happens instead].

[paste code]

What might be causing the wrong output and how should I fix it?

Intermittent Bug

I have a bug that happens intermittently — not every time the code runs. Here are the conditions when it tends to appear: [describe]. Language/framework: [stack]. Here is the code:

[paste code]

What might be causing this and how would I debug it systematically?

Refactoring Prompts

General Refactor

Refactor this code to improve readability and reduce repetition. Keep the same behavior. Language: [language]. Framework: [framework].

[paste code]

Extract a Function

Extract the [describe logic] in this code into a separate, reusable function. Name it [suggested name or "something descriptive"]. Keep the rest of the code unchanged.

[paste code]

Apply a Design Pattern

Refactor this code to use the [pattern name: factory / observer / singleton / strategy / etc.] pattern. Explain what changes you made and why the pattern applies here.

[paste code]

Reduce Complexity

This function has too many responsibilities. Suggest how to split it into smaller functions, each with a single responsibility. Show the refactored version.

[paste function]

Writing Tests Prompts

Write Unit Tests

Write unit tests for this function using [pytest / Jest / RSpec / JUnit / etc.]. Cover: normal cases, edge cases, and at least one error/exception case.

[paste function]

Test for Edge Cases

What edge cases should I test for this function? List the cases and then write tests for each one.

[paste function]

Write Integration Tests

Write integration tests for this module. The module interacts with [describe what it connects to: API / database / file system]. Use [testing framework]. Focus on testing the interaction points, not just internal logic.

[paste code]

Generate Test Data

Generate test data for this schema/function. Include examples for: valid inputs, invalid inputs, boundary values, and empty/null cases.

[paste schema or function signature]

Code Review Prompts

General Code Review

Review this code for: bugs, potential security issues, performance problems, and readability. Be specific about any issues you find — include line numbers or function names. Language: [language].

[paste code]

Security Review

Review this code specifically for security issues. Look for: injection vulnerabilities, improper input validation, insecure defaults, authentication or authorization gaps, and any other security concerns.

[paste code]

Performance Review

Review this code for performance issues. Identify any unnecessary loops, redundant database queries, memory leaks, or inefficient algorithms. Suggest improvements.

[paste code]

Documentation Prompts

Add Docstrings

Add docstrings to all public functions in this file. Use [Google style / NumPy style / JSDoc / etc.] format. Include: description, parameters, return values, and any exceptions raised.

[paste code]

Write a README

Write a README.md for this project. Include: what it does, how to install it, how to use it (with an example), and any configuration options. Language: [language]. Framework: [framework].

[paste relevant code or description]

Explain Code for a Handoff

Write an explanation of this codebase section suitable for a developer who is taking it over. Cover: what it does, how it's structured, the key functions and their roles, and any non-obvious design decisions.

[paste code or describe the section]

Code Explanation Prompts

Explain What Code Does

Explain what this code does, step by step. Assume I understand the programming language but am unfamiliar with this specific logic.

[paste code]

Explain a Specific Function

Explain what this function does, what inputs it takes, what it returns, and what side effects it has (if any).

[paste function]

Explain an Error Message

What does this error message mean? What are the most common causes, and how would I typically fix each one?

[paste error message]

Architecture and Planning Prompts

Plan a Feature

I need to implement [feature description]. Tech stack: [stack]. Existing system: [brief description]. Outline the implementation steps, key components to build, and any potential pitfalls to watch for.

Evaluate Two Approaches

I'm deciding between [approach A] and [approach B] for [problem]. Trade-offs I'm considering: [list]. My priority is [performance / maintainability / speed of implementation / etc.]. Which approach would you recommend and why?

Database Schema Design

Design a database schema for [what it stores]. Requirements: [list key requirements]. Database: [PostgreSQL / MySQL / SQLite / MongoDB / etc.]. Show the tables/collections and their fields with types. Flag any design decisions I should consider carefully.

Tips for Coding Prompts

  • Always specify the language and framework: “Fix this code” is less useful than “Fix this TypeScript function using React hooks.”
  • Include the full error when debugging: The stack trace often contains the key clue.
  • Say what must not change: When refactoring, specify what behavior, naming, or API must stay the same.
  • Ask for explanations: Adding “Explain what you changed and why” to a refactor or debug request gives you better output to learn from.
  • Test all output: Claude cannot run your code. Always test before deploying. See our Claude for Coding guide for safe usage notes.

Related Guides