Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Habits beat tricks

The magic-prompt myth — “Act as a senior engineer with 20 years of Python experience and think step by step…” — is not worth learning. Modern models do not need role-play to be careful, long incantations age badly across versions, and the habit encourages copy-pasting prompts you do not understand.

What survives every model update:

The mental model: prompting is briefing a smart colleague who just walked into the room. They have not seen your screen, do not know your project, and cannot read your mind.

Five habits worth keeping

  1. Goal, then constraints. “I want X. It must work with Y. It must not use Z.”

  2. Show, do not only tell. One example of input and expected output beats two paragraphs of description.

  3. Plan before code. “Outline the steps; I’ll confirm before you write the function.”

  4. Name what you have ruled out. “I tried X — it failed because Y.” Stops it re-suggesting the same thing.

  5. Push back when something smells off. If it references an unfamiliar function, ask it to prove the function exists. Hallucinations do not survive “show me the docs.”

Four ingredients, every time

A prompt that works contains:

Compare with what people actually type: fix my code, make a plot, this doesn't work. The result is a plausible answer to a question you did not ask, about data it invented.

Phrases that carry their weight

“Change nothing else.” Ask for a --dry-run flag without it and you get the flag — plus three renamed variables, reordered imports, added type hints, and a for loop “fixed” into a comprehension. Your two-line change is now a ninety-line diff. And here is the real cost: you will not read ninety lines carefully. You will skim, see nothing alarming, and accept. An unreviewable diff is an unreviewed diff.

“Don’t change anything.” Turns the assistant into a colleague you are thinking out loud with, rather than one who reaches over and edits your file mid-sentence. Use it far more than you expect to.

“Before you change anything: list the files you would touch and what you would do to each. Number them. I’ll tell you which to do.” A plan is cheap to read and cheap to reject. A diff is neither. Half the time the plan reveals it has misunderstood the task, and you have spent fifteen seconds finding that out instead of five minutes reviewing the wrong change. Then: “Do 1 and 3. Skip 2 — that file is generated. Change nothing else.” You have just done code review before the code existed.

“Where the two differ, add a comment instead of choosing for me.” For translation work — SPSS to R, Stata to R, SAS to R, MATLAB to Python, base R to tidyverse. Silent behaviour differences in missing-value handling are exactly how a translated analysis produces a different number and nobody notices for eight months.

Worked example: drafting a function

Underspecified:

write me a script to clean my data

You get generic pandas boilerplate, imaginary column names, assumptions about the file format, and a 100-line answer where 10 would do.

Briefed:

CSV ~50k rows. Columns:
  bike_id, trip_start, duration_min
  (duration_min is numeric but sometimes "NA")

Goal: drop invalid duration_min, parse trip_start,
      keep only the latest trip per bike.

Pandas only. Just the transform — I'll wire
it into my pipeline.

You get a focused eight-line answer, code that runs against your data, and no imaginary columns. The good prompt is barely longer — it just front-loads the things the AI would otherwise guess wrong.

Worked example: debugging an error

“Fix my code” plus eighty pasted lines means the AI will guess what “doesn’t work” means and probably rewrite half the script.

Error, expectation, and attempts:

Running `python analyse.py data/trips.csv`
I get:

  KeyError: 'trip_start'
    at analyse.py:47 in latest_trip()

Expected: a per-bike summary printed to stdout.

The CSV has a column "tripStart" (camelCase).
I tried renaming on read but it didn't take —
not sure why.

Now it can spot the case-mismatch and explain why your rename did not apply.

A second worked case — a real stack trace:

MergeError: Merge keys are not unique in left dataset; not a one-to-one merge

Prompt: “The merge above fails. I expected one row per patient in a (it’s a baseline table) — so the 1:1 validation should pass. What’s the most likely cause, and how do I check?”

A good answer reasons that validate="1:1" failed because a has duplicate patient_ids, tells you to check with a["patient_id"].duplicated().sum(), suggests a patient with two baseline visits as the likely cause, and leaves you the decision: drop the duplicates, or use validate="m:1" if it is expected. Notice it did not silently add validate="m:1" to suppress the error.

Worked example: explaining inherited code

A one-liner everyone has stared at in confusion:

awk -F',' 'NR>1 && $3+0==$3 {sum[$1]+=$3; n[$1]++} END {for (k in sum) print k, sum[k]/n[k]}' data.csv

Prompt:

Explain this awk one-liner line by line.
Then tell me:
  1. what it assumes about the input
  2. how it would break on edge cases
  3. an equivalent in pandas

You get back that -F',' sets the separator, NR>1 skips the header, $3+0==$3 tests that column 3 looks numeric, that it accumulates a sum and count per key in column 1 and prints the mean per key — plus the caveats: breaks on quoted commas, scientific notation may misclassify, no NA handling.

You just got a documented, critiqued, ported version of inherited code in thirty seconds. Paste the explanation back as a comment and commit it.

Ask for examples, not explanations

Regex is the purest case of the trade: fast to generate, impossible to review by eye, catastrophic when subtly wrong.

Explain this, character by character, and give me three test strings
it matches and three it doesn't:

^(?:NL)?(\d{4})[ -]?([A-Z]{2})$

Asking for the strings it matches and the strings it does not turns an unreviewable artefact into something you can check. The same applies to a twelve-verb dplyr chain, a data.table one-liner, a shell pipeline, or a SQL window function. Examples you can verify. Explanations you can only believe.

A development workflow that holds up

  1. Sketch the interface yourself. What is the function name, the inputs, the outputs?

  2. Ask the AI to implement that signature. Not “build me a thing” — fill in the function you defined.

  3. Ask for the edge cases you might have missed. “What inputs would break this?” is surprisingly effective.

  4. Write (or ask for) a test before trusting it. Pin behaviour before you build on top of it.

  5. Commit. So you can compare next time the AI “improves” it.

Explaining is the most underrated use

Most research code is inherited. Reading is the job, and AI is your fastest reading partner. It is brilliant at “explain this function line by line”, “what does this git rebase -i actually change?”, “why is this regex matching unexpectedly?”, “summarise the public API of this file”, and “translate this Stata snippet to R”.

Try it: pick a script you did not write — or wrote six months ago. Paste it with the prompt “Explain what this does. Flag anything suspicious or unidiomatic.” Read critically, add the explanation as a docstring, and commit. You have documented inherited code in five minutes, with AI as your reading buddy, not your author.

When to stop

The doom loop: it suggests a fix, it does not work, you paste the new error, it suggests another, that does not work, and the third fix turns out to be the first fix again.

Two failed attempts is the limit. After that it is guessing, confidently, and you are paying attention to its guesses instead of the problem.

Instead: git restore . to get back to your last checkpoint cleanly. Then either read the code yourself for five minutes, or start a new chat and describe the problem from scratch. A long chat is not accumulated understanding — it is accumulated wrong turns, all still in context. Fresh chat, fresh problem statement. It is free, and it works far more often than the fourth fix.