top of page

Prompt for Code — AI-Assisted Coding, Part 2

  • Shawn West
  • Jul 8
  • 3 min read

Updated: Aug 6

AI-Assisted Coding · Part 2

The difference between an AI coding tool that saves you an hour and one that wastes one is almost never the model — it's the prompt. "Write a sorting function" gets you a generic guess; a prompt that names the language, the input shape, the constraints, and the edge cases gets you code you can actually merge. This walks through prompting for code that works the first time: what context to include, how to be explicit about constraints, and why you always verify.

Vague prompt → vague code. Specific input gets specific output.

Step 1: State the Goal (10 min)

Bad: "Write a sorting function."
Good: "Write a TypeScript function that sorts users by lastName,
       then firstName, both case-insensitive."

The model can't read your mind. Specify:

  • Language

  • Inputs

  • Outputs

  • Constraints

Step 2: Include Context (10 min)

"In this codebase, we use Pydantic models for validation. Here's
an existing one as reference:

```python
class User(BaseModel):
    id: int
    email: EmailStr

Write a similar model for Order with fields: id (int), user_id (int), amount (Decimal, > 0), status (enum: pending/paid/cancelled)."


Models match local conventions when shown them. Less rework.

## Step 3: Be Explicit About Constraints (10 min)

"Function must:

  • Run in < 100ms for 10k items

  • Handle empty list gracefully

  • Not mutate the input

  • Use only stdlib"


Constraints up front. Model designs to them.

Without: model picks freely; you fix later.

## Step 4: Show, Don't Just Tell (10 min)

"Input: [{name: 'Alice', age: 30}, {name: 'Bob', age: 25}]

Expected output: [{name: 'Bob', age: 25}, {name: 'Alice', age: 30}]

Function: sort by age ascending."


Example output anchors the implementation. Fewer "almost right" cases.

## Step 5: Iterate, Don't Restart (10 min)

After first response:

"That's close. Change:

  1. Use type hints (TypeScript)

  2. Handle null inputs

  3. Make case-insensitive"


Don't rewrite the prompt. Iterate.

The model carries context. Use it.

## Step 6: Ask for Tests (10 min)

"Write the function and 5 test cases covering:

  • Empty input

  • Single item

  • Duplicates

  • Pre-sorted

  • Reverse-sorted"


Tests describe behavior. Often: writing tests via AI reveals what the implementation should do.

Then you verify the tests yourself.

## Step 7: Specify the Output Format (5 min)

"Return:

  1. The function code

  2. Tests as a separate code block

  3. Brief explanation"


Without: messy mixed output.

For structured tasks, even more specific:

"Output as YAML matching this schema: ..."


## Step 8: Use the Right Persona (5 min)

"Act as a senior Python engineer who prioritizes simplicity over cleverness."


Personas nudge style. Mileage varies.

Often: "expert in X" with adjectives matters more than the persona itself.

## Step 9: Don't Skip the Why (10 min)

"I'm building a high-traffic API; this function runs on every request. Need it as fast as possible. Memory matters less."


Context → better implementation choices.

Without: model defaults to readability over performance.

## Step 10: Verify (10 min)

The output looks right. Test it:

- Run the code
- Check edge cases
- Read it carefully

LLMs make confident wrong code. Always verify.

Especially security-sensitive code: read every line.

## What You Just Did

Prompting for code: goal, context, constraints, examples, iterate, tests, format, persona, why, verify. Quality prompts.

## Common Failure Modes

**Vague prompt.** Vague code.

**No context.** Code that ignores conventions.

**Don't iterate; rewrite.** Lose carried context.

**Skip verification.** Confident-wrong code ships.

**Over-specified prompt.** Brittle to small changes.

---

## Continue the AI-Assisted Coding path

- **Previous —** [Part 1: Pick an AI Coding Tool](/post/tutorial-1-pick-an-ai-coding-tool)
- **Next —** [Part 3: AI Pair Programming](/post/tutorial-3-ai-pair-programming)

_Part of the **AI-Assisted Coding** learning path._
bottom of page