AI Coding Cheatsheet: Best Prompts for Writing and Fixing Code

Copy-paste prompts that get AI to write production-quality code

Ad placeholder (leaderboard)

How to write coding prompts that work

The single biggest lever in AI coding is specificity. The model cannot read your mind, so every assumption you leave out becomes a guess. A strong prompt states the language and version, the exact inputs and expected outputs, constraints (no external libraries, must be O(n), follow this style), and how success is verified. Provide real code and real error messages rather than paraphrasing them. The more concrete your prompt, the less you have to fix afterwards.

Prompts for writing new code

  • Function generation: “Write a [language] function name(args) that does X. Inputs: …. Outputs: …. Constraints: no third-party deps, handle empty input. Include type annotations and a one-line docstring.”
  • Boilerplate: “Generate a [framework] API endpoint for POST /users with request validation, error handling, and a 201 response. Use [validation library].”
  • Algorithm with tests: “Implement [algorithm] in [language]. Then write three test cases covering a normal case, an edge case, and an empty input.”
  • From a signature: Paste a function signature and comment with “implement this; match the existing code style above.”

Prompts for debugging and fixing

  • Root-cause first: “Here is the code and the exact error. Explain the root cause before suggesting a fix.”
  • Minimal change: “Fix this bug with the smallest possible change. Do not refactor unrelated code. Show me a diff.”
  • Reproduce then fix: “Write a failing test that reproduces this bug, then make it pass.”
  • Explain unexpected behaviour: “This works for input A but fails for input B. Walk through the execution for B and tell me where it diverges.”

Prompts for refactoring, tests, and docs

  • Refactor: “Refactor this for readability without changing behaviour. List the changes you made and why. Keep the public interface identical.”
  • Add tests: “Write [Jest/pytest] tests for this function covering happy path, edge cases, and error handling. Aim for the branches, not just lines.”
  • Document: “Add concise docstrings/JSDoc to each function describing parameters, return value, and thrown errors. Do not change the logic.”
  • Explain code: “Explain what this function does in plain English, then note any bugs or edge cases it does not handle.”

Tool-specific tips

GitHub Copilot works best with descriptive comments above the cursor and good function names — it predicts from surrounding context, so name things clearly. Cursor reads your whole repo; reference files with @filename and ask for multi-file edits. ChatGPT and Claude need you to paste context manually, but reward detailed instructions and multi-turn refinement — ask, review, then say “now also handle the null case.” Across all tools, always read and test what comes back; AI code is a fast first draft, not a finished product.

Ad placeholder (rectangle)