Code Generation Specification

The specification-style prompt for ChatGPT code generation. Explicit function name, inputs, outputs, behavior requirements, and constraints produce far more correct code than descriptive prompts.

Syntax

chatgpt
Write a [language] function:

Name: [exact name]
Input: [param]: [type] — [description]
Output: [return type] — [description]

Behavior:
- [Requirement 1]
- [Requirement 2]
- [Edge case handling]
- Error: [what to return/throw if invalid]

Constraints:
- [No external library]
- [Type requirement]
- [Include/exclude JSDoc]

Example

chatgpt
Write a TypeScript function:

Name: validateAndNormalizeEmail
Input: email: string
Output: { valid: boolean; normalizedEmail?: string; error?: string }

Behavior:
- Trim whitespace before validation
- Validate RFC 5322 simplified pattern
- Normalize to lowercase if valid
- Return error: "Invalid email format" if invalid
- Return normalizedEmail if valid

Constraints:
- No external libraries
- TypeScript strict mode compatible
- Include JSDoc
- No usage examples