By the end of this lesson, you will:
Definition: A prompt is a structured instruction that guides AI to generate specific, high-quality code output. Think of it as programming in English.
Every effective prompt has these components:
[Context] + [Task] + [Constraints] + [Format] + [Examples] = Perfect Output
Poor Prompt:
"make a login form"
Perfect Prompt:
"I'm building a banking app [Context].
Create a secure login form [Task]
using React and TypeScript [Constraints].
Include email validation, password strength meter, and remember me option [Format].
Similar to Chase Bank's login [Example]."
Master this framework for consistent, high-quality outputs:
Provide background information
"I'm building an e-commerce site for handmade crafts"
Specify technical requirements
"Use Python with Flask and SQLAlchemy"
Be specific about what you want
"Create a REST API endpoint that returns the top 10 products"
Clarify potential ambiguities
"Don't use any external APIs, calculate everything locally"
Define structure and style
"Include error handling, comments, and return JSON"
Direct instruction without examples - best for simple tasks
Example:
"Write a function that reverses a string in JavaScript"
When to Use:
Provide examples to guide the output
Example:
"Convert these SQL queries to MongoDB:
SQL: SELECT * FROM users WHERE age > 18
MongoDB: db.users.find({age: {$gt: 18}})
Now convert: SELECT name, email FROM users WHERE status = 'active'"
When to Use:
Break complex problems into steps
Example:
"Build a recommendation system. Let's think step by step:
1. First, analyze user preferences
2. Then, find similar items
3. Finally, rank and return recommendations
Show me the implementation."
When to Use:
Assign a role to the AI
"You are a senior backend engineer at Google.
Review this code for production readiness."
Add helpful limitations
"Explain this concept using only the 1000 most common English words"
Specify exact output structure
"Return the result as:
- Summary (2 sentences)
- Code (with comments)
- Testing instructions (bullet points)"
Bad: "Make it better" Good: "Improve performance by implementing caching and reducing database queries"
Bad: 20-line prompt with nested requirements Good: Break into smaller, sequential prompts
Bad: "Fix the bug" Good: "Fix the null pointer exception in the user authentication flow when email is empty"
Bad: "Create a fast API" Good: "Create an API that responds in under 100ms for 1000 concurrent users"
Prompt 1: Backend
Create a REST API endpoint for user registration:
- POST /api/register
- Validate email format and password strength
- Hash password with bcrypt
- Store in PostgreSQL
- Return JWT token
- Include error handling for duplicate emails
Prompt 2: Frontend
Create a React registration form that:
- Calls the /api/register endpoint
- Shows real-time validation
- Displays password strength
- Shows loading state during submission
- Handles and displays errors gracefully
Prompt 3: Testing
Write tests for the registration feature:
- Unit tests for validation functions
- Integration tests for API endpoint
- E2E test for complete flow
- Use Jest and React Testing Library
✅ Generated code runs without errors ✅ Output matches requirements exactly ✅ Code follows best practices ✅ Includes proper error handling ✅ Has appropriate comments
Try these increasingly complex prompts:
Beginner:
"Create a function to check if a number is prime"
Intermediate:
"Build a weather widget that fetches data from an API and displays it with icons"
Advanced:
"Implement a rate limiter middleware that allows 100 requests per minute per IP,
uses Redis for storage, and returns appropriate HTTP status codes"
Save these templates for instant productivity:
Create a [METHOD] endpoint at [PATH] that:
- Accepts [INPUT FORMAT]
- Validates [FIELDS]
- Performs [ACTION]
- Returns [OUTPUT FORMAT]
- Handles these errors: [ERROR CASES]
Build a [COMPONENT TYPE] component that:
- Uses [FRAMEWORK/LIBRARY]
- Has these props: [PROP LIST]
- Manages this state: [STATE DESCRIPTION]
- Follows [DESIGN SYSTEM] styling
Debug this error: [ERROR MESSAGE]
In this code: [CODE SNIPPET]
Context: [WHAT THE CODE SHOULD DO]
Environment: [TECH STACK]
What I've tried: [PREVIOUS ATTEMPTS]
You now understand the principles of prompt engineering. With these patterns and frameworks, you can generate any code you need. Practice makes perfect - let's apply these concepts in the activities!