Write effective prompts using clear instructions, context, examples, and output format specifications.
Published May 10, 2025
Prompt engineering is the practice of crafting inputs to LLMs to get the best outputs. A well-designed prompt is the difference between a mediocre and an excellent response.
[Role] You are a senior Java developer.
[Context] I'm building a Spring Boot REST API.
[Task] Write a service method that...
[Constraints] Use Java 21 records. Follow SOLID principles.
[Output Format] Return only the Java code in a code block.
❌ Vague: "Write a Java class for users"
✅ Specific:
"Write a Java 21 record class named UserDto with fields:
- id (String)
- email (String, validated)
- name (String)
- createdAt (Instant)
Include a static factory method fromUser(User user)."
❌ No context: "How do I fix this error?"
✅ With context:
"I'm using Spring Boot 3.2 with MongoDB.
I get this error when calling userRepository.save():
[paste exact error]
My User entity is: [paste code]
What's causing this and how do I fix it?"
"Explain the difference between HashMap and TreeMap.
Format:
- First, a one-sentence summary
- Then a comparison table with: time complexity, ordering, null keys
- Finally, one code example showing when to use each"
"Convert these SQL queries to JPQL.
SQL: SELECT * FROM users WHERE age > 18
JPQL: SELECT u FROM User u WHERE u.age > 18
SQL: SELECT name, email FROM users WHERE active = true
JPQL: [convert this one]"
"You are a code reviewer with 10 years of Java experience.
Review this code for:
1. Performance issues
2. Security vulnerabilities
3. Code style (per Google Java Style Guide)
Be critical but constructive. List issues by severity."
System: You are an expert Spring Boot developer.
Generate production-ready code with proper error handling.
Use Java 21 features where appropriate.
Do not add explanatory comments to the code.
User: Create a Spring Boot REST endpoint that:
- Accepts POST /api/v1/users
- Validates: email (not blank, valid format), name (not blank, max 100 chars)
- Creates user in MongoDB
- Returns 201 with the created user
- Returns 409 if email already exists
- Returns 400 for validation errors
❌ "Give me everything you know about microservices"
→ Too broad; answer will be shallow
❌ "Write good code" → No constraints
❌ "Is this correct?" [no code pasted] → Missing context
❌ "Fix all bugs" → Unclear scope
Code Review:
"Review this [language] code for: bugs, performance, security, readability.
[code]
List issues as: [severity] | [line] | [description] | [fix]"
Explain Code:
"Explain what this code does in 3 parts:
1. What it does (1 sentence)
2. How it works (step by step)
3. Potential issues (if any)
[code]"
Debug:
"I expected: [expected]
Actual result: [actual]
Here's the code and error:
[code + error]
What's wrong and how do I fix it?"