Skip to content
Back to AI Coding

Code Generation Prompt Patterns

Patterns for 'write X' prompts: signature first, then body, then tests.

Prompt EngineeringBeginner

Why

GitHub's 2024 survey found 92% of US respondents use AI coding tools to generate test cases, and 73% of open source respondents use AI tools for coding or documentation. Asking for a whole feature at once produces code that's hard to review.

Asking for the signature, then the body, then the tests gives you review points at each step and better-targeted tests.

When

Use for any non-trivial function or module. Skip for one-liners.

How

1. "Write the type signature and doc comment for X; no body." 2. Review the signature; adjust names/types. 3. "Now write the body; handle these edge cases: ...". 4. "Now write tests covering the happy path and each edge case." 5. Run the gate; iterate on red.

Example

Step 1: "type signature for parseConfig(path: string): Config" → export const parseConfig = (path: string): Config => { ... } Step 2: "body; throw on missing required keys; defaults for optional" Step 3: "tests: valid file, missing key, empty file, malformed JSON"

Common Problems

  • ⚠Error: Model returns "I cannot help with that" → rephrase to remove restricted phrasing; narrow scope and provide explicit allowed examples.
  • ⚠JSON parse error on model output → wrap with a fenced ```json block and add "respond with valid JSON only" to the system prompt.
  • ⚠Error: Token limit exceeded (400 Bad Request) → split the prompt, summarize prior context, or switch to a larger context model.

Best Practices

  • ✓Lock the signature before the body to anchor types.
  • ✓Generate tests immediately after the body while context is fresh.
  • ✓Measure the outcome with a concrete metric (time saved, defect rate, test coverage) so the practice can be justified and improved.

FAQ

What is Code Generation Prompt Patterns?

Patterns for 'write X' prompts: signature first, then body, then tests. It helps developers quickly grasp the core value and use cases of Code Generation Prompt Patterns.

How do I get started with Code Generation Prompt Patterns?

To get started with Code Generation Prompt Patterns, focus on these points first: "Write the type signature and doc comment for X; no body."

What are common mistakes with Code Generation Prompt Patterns?

Common mistakes when using Code Generation Prompt Patterns include: Error: Model returns "I cannot help with that" → rephrase to remove restricted phrasing; narrow scope and provide explicit allowed examples.; JSON parse error on model output → wrap with a fenced ```json block and add "respond with valid JSON only" to the system prompt.; Error: Token limit exceeded (400 Bad Request) → split the prompt, summarize prior context, or switch to a larger context model..

What are the best practices for Code Generation Prompt Patterns?

Best practices for Code Generation Prompt Patterns include: Lock the signature before the body to anchor types.; Generate tests immediately after the body while context is fresh.; Measure the outcome with a concrete metric (time saved, defect rate, test coverage) so the practice can be justified and improved..

How do I learn more about Code Generation Prompt Patterns?

Start with the official docs, then search real-world examples on GitHub. Validate patterns in a small scratch repo before applying them to production code.