</>YZIF
Blog

AI Prompt Writing Tips: 5 Ways to Generate Better Code

2026-06-18

Why Prompt Structure Matters

AI code generation is powerful, but the quality of output depends heavily on how you phrase your request. A vague prompt produces vague code. A structured, precise prompt produces production-ready code.

Tip 1: Use Structured Markdown

Instead of writing a single line, structure your prompt with sections:

## Goal
Create a function that validates email addresses

Requirements - Must accept a string input - Return boolean true/false - Support international email formats - No external dependencies

Constraints - Pure JavaScript (no TypeScript) - Must handle edge cases (empty string, null, whitespace)

Tip 2: Specify Output Format

Tell the AI exactly how you want the output structured:

## Output Format
Return a JSON object with:
{
  "function": "the complete function code",
  "testCases": ["array of 5 test cases with expected results"],
  "explanation": "brief explanation of the approach"
}

This forces the AI to think about testing and documentation alongside implementation.

Tip 3: Provide Examples (Few-Shot Prompting)

Show the AI what good output looks like:

## Example
Input: "validate age between 0 and 150"
Expected output format:
{
"function": "function validateAge(age) { return age >= 0 && age <= 150; }",
"tests": ["validateAge(25) → true", "validateAge(-1) → false"]
}

Actual Task Input: "validate email format"

Tip 4: Constrain Scope

Bad prompt: "Write a login system"
Good prompt: "Write a password validation function that checks: minimum 8 chars, at least one uppercase, one number, and one special character. Return an object with { valid: boolean, errors: string[] }."

Before vs After

**Before (vague):**

Write a function to sort an array

**After (structured):**

Write a function that sorts an array of objects by a given key.

Requirements - Input: array of objects and a string key - Output: new sorted array (don't mutate original) - Handle: missing key, null values, nested keys (dot notation) - Support: ascending and descending order

Examples sortBy([{name: "Bob"}, {name: "Alice"}], "name") → [{name: "Alice"}, {name: "Bob"}] sortBy([{a: 2}, {a: 1}], "a", "desc") → [{a: 2}, {a: 1}]

Try the AI Prompt Builder

Use the AI Prompt Builder on YZIF to visually construct structured prompts with condition rules, output formats, and examples — then export as Markdown or JSON.