📡 PROMPT ENGINEERING FOR CODING - Die Kunst der perfekten Prompts

1. Definition

1. Was sind gute Prompts? (Die Basics)

Die Definition:

PROMPT ENGINEERING: Die Kunst, KI so anzuweisen, dass sie EXAKT den Code generiert den du brauchst. Nicht vage, nicht generisch - präzise & konkret!
Bad Prompt: "Generiere einen API Endpoint"
Good Prompt: "Generiere einen GET /users/{id} API Endpoint in Node.js mit Express, Error Handling für invalid IDs, TypeScript, JSDoc Comments auf Deutsch"

Die Restaurant-Analogie (KONKRET):

Bad: Du gehst ins Restaurant & sagst "Mach mir was Leckeres". Koch ist verwirrt - macht Sushi obwohl du Steak wolltest!
Good: "Ich möchte ein Steak, medium-rare, mit Kartoffeln, Salzburg Sauce, kein Salat, weil ich Vegetarisches nicht mag"
Koch weiß GENAU was du willst → perfektes Essen!
KI Parallel: Vage Prompt → chaotischer Code. Präziser Prompt → perfekter Code!

Die 3 Kernelemente eines guten Prompts:

  • 🎯 Kontext: WAS genau brauchst du (nicht "API Endpoint", sondern "GET /users/{id} endpoint")
  • 📋 Constraints: WELCHE Regeln (Language, Framework, Error Handling, Comments, etc.)
  • 🔗 Output Format: WIE soll Output aussehen (JSON? Function? With Tests?)

2. Bad Vs Good

2. Schlecht vs. Gut - Der Crash Course

Vergleich 1: Todo-App API Endpoint

❌ BAD PROMPT:
"Create a todo endpoint"

Output: Random, maybe missing Error Handling, maybe no TypeScript, maybe English comments
Quality: 30%
✅ GOOD PROMPT:
"Create a POST /todos endpoint in Node.js + Express. Requirements:
- Input: {title: string, description: string}
- Validate title is not empty
- Return {id: uuid, title, description, created_at}
- Error if title missing: throw ValidationError
- TypeScript types required
- Comments in German
- Include error handling with try-catch"

Output: Exact code matching ALL specifications
Quality: 95%

Vergleich 2: Database Schema

❌ BAD: "Create database schema"

✅ GOOD: "Create PostgreSQL schema for Social App:
- Table: users (id UUID, name TEXT, email UNIQUE, created_at TIMESTAMP)
- Table: posts (id UUID, user_id FK, content TEXT, likes INT)
- Table: comments (id UUID, post_id FK, user_id FK, text TEXT)
- Add indexes on user_id, post_id
- Add foreign key constraints
- Include migrations for UP/DOWN"

3. Techniques

3. Profi-Techniken (Advanced Prompting)

🎯 Technique 1: Chain-of-Thought
Statt nur "generate code" → "break down steps FIRST"
Prompt: "Generate login function. FIRST explain: 1) What validation needed? 2) How to hash password? 3) What errors possible? THEN generate code"
Impact: KI thinks through problem → besserer Code
📋 Technique 2: Provide Examples
Show KI ein gutes Beispiel von Code-Style du magst
Prompt: "Generate REST API endpoint following this pattern: [show 1 good endpoint example]"
Result: Alle neuen Endpoints follow gleichen Patterns!
🔄 Technique 3: Iterative Refinement
Prompt 1: "Generate basic todo endpoint"
Prompt 2: "Add input validation"
Prompt 3: "Add error handling"
Prompt 4: "Add tests"
Impact: Besserer Qualität als alles auf einmal!
🧠 Technique 4: Few-Shot Learning
Gib KI 3-5 gute Beispiele vom Output du magst
Prompt: "Generate code like these examples: [examples]"
Result: KI lernt EXACTLY dein Style/Pattern!
🎭 Technique 5: Role-Playing Prompts
Prompt: "You are a senior backend engineer. Generate REST API..."
vs. "You are a junior developer. Generate REST API..."
Impact: KI ändert Output basierend auf Role! (Senior = Better Quality)

4. Examples

4. Real-World Prompts (Copy-Paste Ready)

Prompt Template 1: REST API Endpoint
"Generate a [METHOD] /[endpoint] API endpoint in [language] using [framework]. Requirements:
- Input parameters: [list]\br/> - Validation rules: [rules]\br/> - Error handling: [errors]\br/> - Return format: [format]\br/> - Code style: [style rules]\br/> - Comments language: [German/English]\br/> - Include TypeScript types"
Prompt Template 2: Database Migration
"Generate [language] database migration for [ORM] that:
- Creates table [name] with columns [list]\br/> - Adds indexes on [columns]\br/> - Adds foreign keys to [tables]\br/> - Includes DOWN migration for rollback\br/> - Uses [naming convention]"
Prompt Template 3: Component/Module
"Generate a [component type] in [language]/[framework] that:
- Implements interface [interface]\br/> - Handles these inputs: [list]\br/> - Throws these errors: [errors]\br/> - Includes unit tests\br/> - Follows this code style: [example]\br/> - Add JSDoc comments in [language]"

5. System Prompts

5. System Prompts (Die Langfrist-Lösung)

Was ist ein System Prompt?

Eine Basis-Instruktion die KI IMMER followet, egal welcher User Prompt kommt!

Beispiel System Prompt:
"Du bist ein Senior Backend Developer spezialisiert auf Node.js + PostgreSQL. Für alle Code-Generation folge STRICT RULES:
1. ALWAYS use TypeScript with strict mode
2. ALWAYS add JSDoc comments in German
3. ALWAYS use camelCase for variables
4. ALWAYS handle errors with try-catch + specific error types
5. ALWAYS return {success: bool, data: object, error: string} format
6. ALWAYS add input validation
7. ALWAYS use logger instead of console.log"

Impact: User braucht nur sagen "Generate login endpoint" → KI generiert mit ALL diese Rules!

System Prompt vs. User Prompt:

  • System Prompt: Setup Rules (global, langfristig)
  • User Prompt: Specific Task (local, einmalig)
  • Together: System Prompt = Foundation, User Prompt = specific request

6. Advanced

6. Advanced Strategies (2025 Best Practice)

🚀 Strategy 1: Batch Prompting

1 komplexer Prompt statt 100 einfache Prompts
Prompt: "Generate 100 API endpoints following pattern [template]. Inputs: [CSV mit 100 endpoint names]"
Result: 100x schneller, 100x konsistenter!

🔄 Strategy 2: Prompt Chaining

Prompt 1: Generate Architecture
Prompt 2: "Based on architecture [output], generate database schema"
Prompt 3: "Based on schema [output], generate API endpoints"
Result: Each stage buildet auf previous output!

📊 Strategy 3: Evaluation-Driven

1. Generate code with Prompt A
2. Evaluate quality (run tests, check coverage)
3. If <80% pass → Refine prompt based on failures
4. Generate again with improved Prompt B
Result: Prompts improve over time!

🎯 The Brutal Truth:

PROMPT ENGINEERING IST 50% DER AI CODE GENERATION!

Bad Prompt → Chaotic Code (need 10 iterations)
Good Prompt → Perfect Code (first try)

Time Investment: 10 min writing perfect prompt → saves 2 hours manual fixing

2025 Pro-Tip: Companies spend 20% time on PROMPTS, 80% time on DEPLOYMENT. Beginners spend 20% on deployment, 80% fixing bad code from bad prompts!