Stage 7 — Prompts for Code (Core Skill)

This is where prompt engineering becomes practical software engineering.

Stage topics

  • Code generation with requirements
  • Refactoring
  • Bug finding
  • Test generation
  • Documentation from code

Code generation

Reliable coding prompt should include:

  • language/version,
  • architecture constraints,
  • style conventions,
  • acceptance criteria,
  • output format (files, diff, change list).

Refactoring

Avoid “make it better”. Request:

  • explicit code smells,
  • target structure,
  • no behavior change,
  • required tests after refactor.

Bug finding

Effective pattern:

  • context (symptom, logs, stacktrace),
  • cause hypotheses,
  • minimal reproduction steps,
  • fix + explanation of correctness.

Test generation

Always specify:

  • test type (unit/integration),
  • mocking boundaries,
  • edge cases,
  • expected invariants.

Documentation from code

Useful outputs:

  • API contracts,
  • ADR summary,
  • support runbook,
  • release changelog.

Prompt pipeline

Coding prompts as engineering specifications

A coding prompt should behave like a compact engineering ticket. The model needs to know the target repository, the language version, the framework, the ownership boundary, and the definition of done. Without that, it may produce code that looks correct in isolation but does not fit the existing project.

For bug finding, the prompt must distinguish symptoms from causes. Logs and screenshots are symptoms. The fix should target the cause and explain why the symptom disappears. For refactoring, the prompt must explicitly preserve behavior, because “clean up” can otherwise become an accidental rewrite. For tests, the prompt should define which layer is under test and what must be mocked, otherwise generated tests often become brittle or too broad.

Task type Prompt must include Acceptance signal
Code generation Version, framework, file scope, constraints Builds and meets behavior
Refactoring Smell, target shape, no behavior change Tests still pass
Bug finding Symptom, reproduction, suspected area Cause is explained and fixed
Test generation Layer, mocks, edge cases Meaningful failure on regression

Stage takeaway

Coding prompts must be highly specified.
Less ambiguity means less technical debt after generation.

Beginner explanation

A coding prompt is not a request to “write code”. It is a compact engineering specification. The model needs to know language and version, existing libraries, files in scope, behavior that must not change, and how completion should be verified. Without that, the model may create a nice isolated snippet that does not fit the project.

For a new feature, the prompt should include acceptance criteria. Example: “add endpoint POST /orders, validate quantity >= 1, return 400 on validation error, add unit tests for service and integration test for controller.” This defines not only code, but expected behavior.

For bug fixing, separate symptom from cause. Stack trace, log, and screenshot show symptoms. A good prompt asks for root cause, minimal fix, and explanation of why the fix removes the cause. For refactoring, explicitly say “do not change public behavior” and ask for tests or risk notes.

In a Spring Boot 3 project, fix the bug in OrderService.
Symptom: an order is created when quantity=0.
Expected behavior: return validation error before saving.
Do not change API response format.
Add tests for quantity=0 and quantity=1.
In the answer, list changed files and verification.

Mini scenarios from real projects

  • Model generates compiling code that ignores project constraints: prompt does not specify versions and conventions.
  • Bug-finding output is superficial: findings format with severity and line references is not required.
  • Test generation covers happy path but misses edge cases: negative scenarios are not explicitly requested.

Fast decision rules

  • For code tasks, always specify environment context: language, versions, style, constraints.
  • For reviews, require structured findings and risk-based prioritization.
  • For tests, require happy-path, negative-path, and boundary-case coverage.
Glossary
LLM
A large language model trained to process and generate text or other token sequences.
prompt
The input instructions and context given to a language model.
context window
The maximum amount of input and output tokens a model can consider at once.
system prompt
High-priority instructions that define model behavior for a conversation or task.
instruction
A directive that tells a model or program what to do.
role
A prompt element that frames the model's perspective, responsibility, or behavior.
constraint
A rule or limitation that narrows what an answer or system is allowed to do.
few-shot
A prompting style that gives the model a few examples before asking it to solve a task.
zero-shot
A prompting style where the model receives no examples for the specific task.
chain of thought
Step-by-step reasoning used to work through a problem.
hallucination
Model output that sounds plausible but is false, unsupported, or fabricated.
grounding
Connecting model output to provided context, data, documents, or verified sources.
RAG
Retrieval-Augmented Generation, a pattern where relevant external information is retrieved before generation.
retrieval
Finding relevant information from documents, databases, or tools before answering.
citation
A reference to the source that supports a statement or answer.
evaluation
Measuring output quality against criteria, examples, tests, or expected behavior.
iteration
Repeating a process with improvements based on feedback or results.
tool calling
A model capability where the model requests an external tool or function to perform an action.
function calling
A structured form of tool calling where the model selects a function and provides arguments.
prompt injection
An attack or failure mode where untrusted text tries to override intended instructions.
token
A unit of text or data that a language model processes inside its context window.