Code Prompt Cheatsheet

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

By Operator 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

PatternExample promptCheck for
Simple functionWrite a Python 3.11 function that reverses a string. Add type hints and a docstring.Check Unicode behavior, type hints, documentation, and tests.
API endpointGenerate a Node.js Express route for a /login endpoint with JWT authentication.Check authentication boundaries, input handling, errors, and tests against current documentation.
Data transformationWrite 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

PatternExample promptCheck for
Error explanationHere 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 debuggingDebug this SQL query step by step. Suggest a corrected version.Confirm the diagnosis and run the query against representative test data.
Node.js runtimeReferenceError: 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

PatternExample promptCheck for
ModernizationRefactor this JavaScript code to use async/await instead of callbacks.Compare error propagation and return behavior before and after the change.
OptimizationOptimize this Python function for readability and performance.Require measurements and reject changes supported only by intuition.
Style enforcementRefactor this Java class to follow Google Java Style.Run the applicable formatter and inspect semantic changes separately.

Documentation

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

Test generation

PatternExample promptCheck for
Unit testsWrite pytest unit tests for this function, covering edge cases.Check assertions, boundary cases, and failure against the unmodified defect.
Java testsGenerate JUnit tests for this Java method.Check the JUnit version, fixtures, assertions, and error cases.
Integration testsCreate 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