Skip to content
Back to AI Coding

Function Generation Patterns

Generate a function from a signature + doc + examples; gate with types and tests.

Code GenerationBeginner

Why

GitHub's 2024 Octoverse reported a 59% surge in contributions to generative AI projects and a 98% increase in AI projects overall on the platform. A signature + doc + examples fully specifies most functions.

Generating the body from that spec produces code that matches your types and intent, with tests that prove it.

When

Use for any pure or mostly-pure function with a clear signature. Skip for UI/stateful code.

How

1. Write the signature + JSDoc with @param/@returns and an example. 2. Prompt: "implement per the doc; handle these edge cases: ...". 3. Ask for tests alongside (happy path + each edge). 4. Run tsc + tests; iterate on red. 5. Reject any signature change unless you approve it.

Example

/** * Parse a page query param into a 1-based page number. * @param input - raw string from ?page= * @returns 1-based page (min 1), or 1 if invalid * @example parsePage("3") // 3 */ export const parsePage = (input: string | null): number => { ... } Prompt: "implement per doc; edge cases: null, '', '0', '-1', 'abc', '999'"

Common Problems

  • ⚠Generated code fails to compile: "SyntaxError: Unexpected token" → ask the model to output a complete file with correct imports; verify with `npx tsc --noEmit`.
  • ⚠Error: Hallucinated API: model calls a function that doesn't exist → cross-check against the actual library docs and pin the version in package.json.
  • ⚠Error: Cannot find module 'xxx' → `npm install xxx`; generated code often imports packages not yet in the project.

Best Practices

  • ✓Specify the signature and an example before the body.
  • ✓Generate tests in the same turn 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 Function Generation Patterns?

Generate a function from a signature + doc + examples; gate with types and tests. It helps developers quickly grasp the core value and use cases of Function Generation Patterns.

How do I get started with Function Generation Patterns?

To get started with Function Generation Patterns, focus on these points first: Write the signature + JSDoc with @param/@returns and an example.

What are common mistakes with Function Generation Patterns?

Common mistakes when using Function Generation Patterns include: Generated code fails to compile: "SyntaxError: Unexpected token" → ask the model to output a complete file with correct imports; verify with `npx tsc --noEmit`.; Error: Hallucinated API: model calls a function that doesn't exist → cross-check against the actual library docs and pin the version in package.json.; Error: Cannot find module 'xxx' → `npm install xxx`; generated code often imports packages not yet in the project..

What are the best practices for Function Generation Patterns?

Best practices for Function Generation Patterns include: Specify the signature and an example before the body.; Generate tests in the same turn 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 Function Generation 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.