Prompting for Code
How to get clean, working, production-ready code from AI.
Getting AI to write code for you is one of the most powerful vibe coding skills. However, a vague prompt produces vague, buggy code. A precise, structured prompt produces clean, production-ready code. Here's the formula to always get great code from an AI.
The Perfect Code Prompt Formula
Use this structure every time:`
[ROLE] You are an expert [language/framework] developer.
[CONTEXT] I am building [describe your project].
[TASK] Write a function/component that [clearly describe what it does].
[INPUT/OUTPUT] It takes [input] and returns [output].
[CONSTRAINTS] Use [technology]. Do NOT use [what to avoid]. Handle [edge cases].
[FORMAT] Return only clean code with comments. No explanations.`
Example: Prompting for a Python Function
Here's a real example of a high-quality code prompt:
You are an expert Python developer.
I am building a REST API for a blog platform.
Write a Python function called `slugify` that:
- Takes a string (blog post title) as input
- Converts it to a URL-friendly slug
- Handles special characters, spaces, and unicode
- Returns a lowercase string with hyphens instead of spaces
Requirements:
- Do NOT use any external libraries (only Python standard library)
- Handle edge cases like multiple spaces and leading/trailing hyphens
- Include type hints and docstring
Return only the function code.Debugging with AI
When debugging, always include: the full error message, the relevant code snippet, and what you've already tried. Say: 'I tried X and Y. Here is the full error: [paste error]. Here is the code: [paste code]. What is wrong?'
