Code Prompt Cheatsheet

Quick-reference prompts for coding: debugging, refactoring, documentation, and code generation.

By Chat Overview Published Updated
Coding prompt
Code
Failure
Version
Output
Check the change
  • Inspect
  • Diff
  • Test

How to use this sheet

Choose a pattern, add the relevant code and environment, and request a result that can be inspected. See Prompt Engineering for Code for the full method.

Code generation

Pattern Example prompt Check for
Simple function Write a Python 3.11 function that reverses a string. Add type hints and a docstring. Check Unicode behavior, type hints, documentation, and tests.
API endpoint Generate a Node.js Express route for a /login endpoint with JWT authentication. Check authentication boundaries, input handling, errors, and tests against current documentation.
Data transformation Write a pandas script that groups a data frame by category and calculates average sales. Check missing values, numeric types, grouping behavior, and output order.

Debugging

Pattern Example prompt Check for
Error explanation Here is my error: TypeError: cannot read property 'map' of undefined. Explain the cause and fix. Require the value's origin and a reproducing case before selecting a fix.
SQL debugging Debug this SQL query step by step. Suggest a corrected version. Confirm the diagnosis and run the query against representative test data.
Node.js runtime ReferenceError: fetch is not defined. Explain the cause step by step and show how to fix it. Check the Node.js version, module system, and available built-in API before adding a dependency.

Refactoring

Pattern Example prompt Check for
Modernization Refactor this JavaScript code to use async/await instead of callbacks. Compare error propagation and return behavior before and after the change.
Optimization Optimize this Python function for readability and performance. Require measurements and reject changes supported only by intuition.
Style enforcement Refactor this Java class to follow Google Java Style. Run the applicable formatter and inspect semantic changes separately.

Documentation

Pattern Example prompt Check for
Docstrings Add docstrings to this Python class using Google style. Check arguments, return values, exceptions, and behavior against the code.
Inline comments Write comments explaining non-obvious decisions in this algorithm. Remove comments that only restate syntax and verify the remaining explanations.
README Write a README.md for this project that explains installation, usage, and testing. Check every command, version, path, and stated prerequisite.

Test generation

Pattern Example prompt Check for
Unit tests Write pytest unit tests for this function, covering edge cases. Check assertions, boundary cases, and failure against the unmodified defect.
Java tests Generate JUnit tests for this Java method. Check the JUnit version, fixtures, assertions, and error cases.
Integration tests Create integration tests for this REST API using Postman collection syntax. Check environment variables, authentication, cleanup, and negative cases.

Best practices

  • Specify the language and version.
  • Provide error messages, code snippets, and environment details.
  • Request the diagnosis and assumptions before the patch.
  • Review the diff and run tests before production use.

Further reading