Write a coding prompt that gets reviewable code

Módulo 1: Code with AI, and check every line

Conteúdo do curso

Esta aula ainda não está no seu idioma, então aqui está em inglês.

Teste rápido

Guess first, no penalty. A developer types: "Write a Python function that removes duplicate emails from a list." What is missing?

A prompt is the message you send to the AI. For code, a vague prompt gets code that runs but makes choices you didn't make. An edge case is an unusual input, such as an empty list, that breaks code written only for normal input.

A coding prompt that gets reviewable code has four parts:

  • Task: one verb and one job. "Write a function that...", "Add a test for...".
  • Context: where the code runs, the data it gets, and the language version.
  • Rules: style, error handling, and limits like "no new dependencies" (outside packages your code would need to install).
  • Return: what you want back. Ask for the code, the edge cases it handled, and tests.

Asking for edge cases and tests is the trick. It makes the model show its assumptions, so you review choices instead of hunting for them.

A worked example

Task: Write a Python 3.12 function dedupe_emails(emails: list[str]) -> list[str].

Context: Input comes from a CSV upload. Emails may have spaces or capitals.

Rules: Keep the first time each email appears, in the original order.
Treat "[email protected]" and "[email protected] " as the same. Skip blank strings.
No new dependencies. Raise TypeError if the input isn't a list.

Return: the function, a list of edge cases you handled, and 4 pytest tests.

Preencha as lacunas

Fill the gaps.

Prompt

Task: . Rules: . Return: the function, .

Your turn:

Task: Write [what] that [does what].
Context: It runs in [where]. Input looks like [example]. Language: [version].
Rules: [style], [error handling], [no new dependencies?]
Return: the code, the edge cases you handled, and [N] tests.

Try it

Seus passos, cerca de 2 minutos

0 de 1
  1. 1.Write the prompt for a small function you need this week, and compare its edge cases with yours. Add the template to your AI code review kit.2 min

Exercícios

Pergunta 1 de 3

You ask an AI for 'a function that formats prices'. It returns one that rounds down, adds a dollar sign and crashes on None. What is the best fix to your prompt?

Tutor de IA

Tire dúvidas sobre esta aula e tenha ajuda na tarefa do “Experimente”. O tutor faz parte do Learn Pro.

Ver o Learn Pro