Prompt Engineering for Code Generation
Best practices for prompting models to produce cleaner, more secure, and modular code.
Prompting for code is different from prompting for creative writing. You need to focus on constraints, patterns, and strict structure to ensure the model doesn't produce "spaghetti code" or insecure patterns.
Constraint-Based Prompting
Explicitly define the expected architecture. Use prompts like: "Write a modular, testable service in TypeScript. Follow DDD principles, define interfaces for each dependency, and use explicit error handling." The more specific you are about your architectural standards, the less "boilerplate garbage" the model will generate.
The Contextual Advantage
Provide the model with the interfaces of existing files. If you are writing a new function, include the `types.ts` or `schema.prisma` file in the prompt so the model understands exactly what it needs to integrate with.