Lab · Programming · 5 sections

Generate and review code with AI

Give an AI assistant a testable specification, inspect its draft and challenge plausible errors.

No account needed to read online.

What it covers

  • Draft the acceptance example yourself
  • Give a bounded prompt
  • Review a plausible draft
  • Review the revision as a change

STATISTICAL PROGRAMMING LAB

Who had a return visit within 30 days?

Six people, two groups, and a visit log with repeat visits. Build an endpoint, compare risks, then audit three proposed pieces of code.

Inspect the source tables

Day 0 is enrollment. Follow-up is the number of days with complete capture of return visits. The visit log can include records outside the endpoint window.

cohort

idarmfollowup
1A30
2A30
3A12
4B30
5B30
6B30

visits

idday
18
112
231
430
60

SQL workbench

Return two rows, before and after the inner join to visits. Report stage, row_count and unique_people. Check which people disappeared or appeared twice.

Explain the group comparison

With 12 days observed, person 3 has an unknown outcome. Group A has one event among two observed people (50%). With 30 days and no visit, that person becomes an observed zero and group A falls to one in three. Group B stays at one in three. The change comes entirely from outcome ascertainment.

Code review desk

SELECT arm, AVG(event) AS risk
FROM outcomes JOIN visits USING (id)
GROUP BY arm;

Which issue or review action matters here?

Read online

Generate and review code with AI

Draft the acceptance example yourself

Before asking an AI assistant to write code, formulate the task yourself. Define the input schema, row unit, intended result, missing-value policy, software constraints, and a small example with an answer you calculated by hand. This gives you an independent reference for evaluating the draft.

Give a bounded prompt

Ask for one small, bounded change. Request assumptions, code, tests, and a brief explanation of edge cases. Specify the runtime and permitted dependencies. Do not paste private, licensed, or identifiable data into an AI tool unless its use is approved for that data.

Write a Python 3 function using only the standard library.
Input: a list of 0, 1 or None, one outcome per participant.
Return event count, observed count and observed proportion.
If nothing is observed, return None for the proportion.
Reject other outcome values. Explain how zeros are handled.
Show tests for a mix of outcomes, all missing, and an invalid value.

Review a plausible draft

This plausible draft keeps only truthy values. In Python, zero and None are both false, so the output becomes [1, 1] and the risk becomes 1.0 rather than 0.5. Predict the failure before running it, then write a regression test that fails on this draft.

values = [1, 0, None, 0, 1]
kept = [value for value in values if value]
print(kept)  # [1, 1]

Review the revision as a change

Review the AI response as a proposed code change. Read every line. Check the diff for unrelated changes. Run the hand-worked example and boundary tests independently. Confirm that errors are explicit, output types are stable, and the code matches the specification. Ask the model to explain a line or propose a counterexample, but verify its claims yourself. Keep the prompt and review decision when they are needed for an audit trail.

Practice the full review loop

Ask an AI assistant for a function that summarizes the five outcomes and tests. Save its answer to a temporary branch. Add the zero-containing example before accepting the code. Run the tests, inspect the diff, and fix any failure. In a short review note, state what you checked, what changed, and which claims you verified independently. Do not treat generated tests as independent evidence if they simply repeat the generated implementation.

Discussion

Questions, clarifications and applications of this resource.

Loading discussion…