As developers, we’re used to deterministic systems.
Input A + function f = output B. Every time.
Large Language Models don’t work like that. They’re probabilistic. The same input can produce different outputs, and prompts that are almost right can lead to results that are completely wrong.
That’s not magic. And it’s not luck either.
It’s engineering.
Prompt engineering is simply the skill of giving the model enough direction to consistently get useful results, especially when you’re coding, reviewing, refactoring, or designing systems with AI.
This article is a practical guide to the techniques that actually make a difference, plus the mistakes that waste the most time.
Content
- The Golden Rule: Context Beats Length
- Core Techniques
- Prompt Design Patterns
- Common Anti-Patterns (And Why They Fail)
The Golden Rule: Context Beats Length
If you remember just one thing, make it this:
Output quality depends on context quality, not on prompt length.
A short, well-anchored prompt will almost always outperform a long wall of vague instructions. Before you ask how, make sure the model understands what and why.
Think of it like onboarding a new teammate. You wouldn’t dump a backlog on them and say “figure it out”.
Core Techniques
1. Zero-Shot Prompting (The Baseline)
This is the simplest case: no examples, just a clear request. It works surprisingly well for tasks the model already understands deeply.
Use it when:
- The problem is common
- The transformation is straightforward
- You’re not enforcing a strict style or architecture
Example: converting a JPA entity into a Java Record.
@Entity
@Table(name = "products")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank(message = "Name is required")
@Size(min = 3, max = 100)
private String name;
@Positive
private BigDecimal price;
}
Prompt
Convert this JPA Entity to a Java 21
record, preserving Bean Validation annotations.
Result
public record Product(
Long id,
@NotBlank(message = "Name is required")
@Size(min = 3, max = 100) String name,
@Positive BigDecimal price
) {}
No ceremony. No overthinking. This is your default.
2. Few-Shot Prompting (Style Calibration)
Few-shot prompting is how you teach the model your conventions without explaining them.
You show it a couple of examples and say: “Do more of this.”
Use it when:
- Enforcing naming conventions
- Following internal architecture patterns
- Generating repetitive but structured code
Prompt
Example 1:
Input: find user by id
Output: public UserDTO findById(Long id)...
Example 2:
Input: delete product by code
Output: public void deleteByCode(String code)...
Task:
Now follow the same pattern.
Input: update inventory status
Result
public void updateInventoryStatus(String status)...
This works because the model is very good at pattern matching, often better than it is at following long explanations.
3. Chain-of-Thought (When Logic Matters)
Some problems fail not because the model can’t code, but because it jumps to conclusions too fast.
Chain-of-Thought prompts slow it down.
You explicitly ask the model to reason about the problem before writing code.
Use it when:
- Refactoring legacy code
- Designing abstractions
- Making architectural decisions
public double calculate(double v, int t) {
if (t == 1) return v * 0.1;
else if (t == 2) return v * 0.05;
else if (t == 3) return v * 0.2;
else return 0;
}
Prompt
Analyze what this method is doing and what
vandtrepresent. Then suggest how to refactor it using the Strategy Pattern. Finally, write the refactored code with meaningful names.
The key here isn’t the code. It’s forcing the model to align on intent before implementation.
4. Iteration and Self-Critique
One of the most underrated techniques: ask the model to review itself.
Use it when:
- Reviewing security
- Looking for edge cases
- Improving robustness
Flow
- Generate a solution
- Switch roles
- Critique and improve
Prompt
Now act as a security auditor. Identify three vulnerabilities and rewrite the code securely.
This works because the model applies a different evaluation lens instead of blindly extending its first answer.
Prompt Design Patterns
Persona Pattern
Give the model a role with a clear point of view.
public void process() {
synchronized(lock1) {
synchronized(lock2) {
// business logic
}
}
}
Prompt
Act as a Principal Engineer focused on concurrency and performance. Review this code and identify risks.
This consistently produces sharper, more opinionated feedback than a generic “review this code”.
Template Pattern
When you want predictable structure, don’t ask nicely, enforce it.
function login(u, p) {
if (u == 'admin' && p == '123') return true;
return false;
}
Prompt
Analyze this code and respond using exactly this format:
- Problems detected
- Suggested improvements
- Refactored code
Templates reduce ambiguity and make outputs easier to scan, diff, or automate.
Context Anchors (IDE-Driven Workflows)
When working inside tools like Cursor, referencing files explicitly keeps the model grounded.
Based on
@.cursorrulesand the structure of@UserInterface.java, implement@UserServiceImpl.java.
This prevents the model from “freestyling” outside your codebase conventions.
Common Anti-Patterns (And Why They Fail)
1. Walls of Text
Models lose focus when everything feels equally important.
Fix: Structure your prompt. Markdown helps more than people expect.
2. Vague Constraints
“Make it clean” is not a requirement.
Fix: Be explicit. “Java 21, no external libs, immutable design, @ControllerAdvice for errors.”
3. Leading Questions
“This code is thread-safe, right?” invites agreement, not analysis.
Fix: “Analyze this code for thread-safety issues and justify your conclusion.”
4. Prompt Leakage
Logs, tokens, credentials: don’t paste them.
Ever.
Learn More
For a comprehensive database of techniques and latest research, I highly recommend checking out: Prompt Engineering Guide
Final Thought
Prompt engineering is about thinking clearly, expressing intent, and treating the model like a powerful (but very literal) collaborator.
If you do that, AI stops being a toy and starts becoming a real part of your engineering workflow.
Happy prompting 👋

Leave a Reply