5 Essential Prompting Principles for Programming with AI
The article outlines five practical prompt‑engineering rules—state the goal first, give full context, set clear constraints, provide concrete examples, and ask step‑by‑step—to help developers communicate effectively with AI for coding tasks.
Principle 1: State the goal before details
❌ Bad example: "Help me look at this code" – the AI does not know whether you want an explanation, optimization, or bug‑finding.
✅ Good example: "This code runs in a concurrent scenario and occasionally causes duplicate charges; help me locate the race condition and fix it." The clearer the goal, the more accurate the answer.
Principle 2: Provide context
AI cannot read your mind; you must supply the necessary background such as file names, functions, data scale, and existing technologies.
❌ Bad example: "Why is this API slow?"
✅ Good example: "The endpoint (UserService/UserController.java getList) averages 800 ms with 50 QPS, using PgSQL + Redis; help me locate the bottleneck." Context = file + function + data size + known information.
Principle 3: Add constraints
Without constraints the AI may "free‑form" solutions.
❌ Bad example: "Write a login API."
✅ Good example: "Using Spring Boot 3 + JWT, write a login API with the following constraints:"
Path /api/login, POST, body {username, password} Password validated with BCrypt
No new dependencies; reuse the existing Result wrapper
Provide a corresponding JUnit test
More constraints lead to less rework.
Principle 4: Show examples instead of describing
If you want a certain style, paste a snippet that demonstrates it.
@RestController
@RequestMapping("/api/users")
public class UserController {
...
}One concrete example can replace a 200‑word description because the AI’s imitation ability is strong.
Principle 5: Ask step‑by‑step, not all at once
❌ Bad example: "Help me build an e‑commerce system."
✅ Good example:
Design the order state machine. Write the database schema. Implement the order‑placement API.
Breaking complex tasks into small, verifiable steps reduces errors and aligns with mechanisms like Plan Mode or Sub‑agent.
Template: Universal programming prompt structure
【Goal】
I want …
【Context】
- Project: XXX
- File: @xxx/yyy.ts
- Current state: …
【Constraints】
- Do not introduce library XXX
- Follow existing XXX style
- Add XXX tests
【Output format】
- Provide the full modified file / a diff / list of changesFollowing this template raises the hit‑rate immediately.
Additional tips
1. Let it restate first
"Before you start, tell me in one sentence how you plan to modify the code."
This avoids direction drift.
2. Ask it to list alternatives
"Give me 2–3 solutions with pros and cons, and I will choose."
This prevents the AI from forcing a decision.
3. Let it question you
"If any part of my description is unreasonable, point it out first."
Helps uncover unclear requirements.
4. Use the "think" keyword
Appending think hard or think deeper in Claude / Codex triggers deeper reasoning.
Summary
State the goal before details.
Provide full context as the foundation.
More constraints mean less rework.
One concrete example replaces lengthy description.
Break complex tasks into small steps.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
