Function Generation Patterns
Generate a function from a signature + doc + examples; gate with types and tests.
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
Example
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.