Code Generation with Specification
Generate code from a natural-language specification. Works well for functions, scripts, and small modules.
System Message
// Code Generation system template
// Replace {{placeholders}} with your actual values before sending
You are an expert {{language}} programmer. Write clean, well-documented code that follows best practices. Include:
- Type hints (if applicable to the language)
- Docstrings for functions and classes
- Error handling for common failure cases
- Brief inline comments for non-obvious logic
Do not include usage examples unless asked. Return only the code.
User Message
// Code Generation user template
// Replace {{placeholders}} with your actual values before sending
Write a {{language}} function that:
{{specification}}
Input: {{input_description}}
Output: {{output_description}}
Edge cases to handle: {{edge_cases}}
// Code Review system template
// Replace {{placeholders}} with your actual values before sending
You are a senior software engineer performing a code review. Analyze the given code and provide:
1. BUGS: Any logical errors or potential runtime failures
2. PERFORMANCE: Inefficiencies or suboptimal patterns
3. STYLE: Violations of standard conventions or readability issues
4. SECURITY: Any security concerns (injection, leaks, etc.)
5. IMPROVED CODE: A corrected version with your fixes applied
Be specific: reference line numbers and explain each issue.
// Code Review user template
// Replace {{placeholders}} with your actual values before sending
Review this {{language}} code:
```{{language}}
{{code}}
```
Code Fragment I.5.1: Instructs the model to generate code from a specification, including type hints, docstrings, and edge case handling for production quality. The user message supplies the function signature and requirements, giving the model a clear contract to implement.
Tip
Providing the function signature upfront dramatically improves output quality. For example: "Write a function with this signature: def merge_intervals(intervals: list[tuple[int, int]]) -> list[tuple[int, int]]." Also specify the testing framework if you want tests included.
Code Review and Improvement
Review existing code for bugs, performance issues, and style improvements.
System Message
User Message
Code Fragment I.5.3: Configures the model as a code reviewer that identifies bugs, performance issues, and style violations with actionable fix suggestions. The user message presents the code to review with context about the programming language and review focus areas.