Week 9 · Thursday, Nov 19 · Lecture
| Time | What |
|---|---|
| 1:30 | Tuesday is code review: what “ready” means |
| 1:35 | Recommendations: ten habits of good research code |
| 2:00 | AI audit #2: review the example repository |
| 2:25 | Using AI with rules: AGENTS.md, STYLE.md, short prompts |
| 2:50 | AI ethics discussion |
| 3:00 | Before Tuesday |
Learning goals: name and apply habits that make code readable, checkable and reproducible; find their violations in code that runs; write an instruction file that sets boundaries for an AI assistant; keep prompts short by putting stable context in files.
No AI in today’s audit or discussion.
Important
Tuesday’s lecture is the peer review (5%). You review a classmate’s repository in class, written by you, no AI, submitted before you leave. The assignments are posted on Ed on Monday.
Your repository must be review-ready by Tue 1:30 pm:
| # | Habit | One-line test |
|---|---|---|
| 1 | One folder, one job. Raw data is read-only | Could you delete output/ and get it back? |
| 2 | Names say what a thing is | Would a stranger know what df2 holds? |
| 3 | No magic numbers | Does 2007 appear in more than one place? |
| 4 | Don’t repeat yourself | Did you copy a block a second time? |
| 5 | Comments explain why, and stay true | Does the comment still match the code? |
| 6 | Fail loudly | Does a broken assumption stop the render? |
| 7 | Missing values are a decision | Do you know how many rows each step dropped? |
| 8 | Relative paths, fresh session | Does it render from a fresh clone? |
| 9 | Let tools check style | Have you run lintr / styler? |
| 10 | Small commits that say what changed | Could you find the commit that broke it? |
A number that means something gets a name, once. Change the threshold in one place, and every use changes with it. In a runbook, the name can be a Quarto parameter (params: in the YAML header, params$adult_age in R).
Before: the same block three times. The third copy already differs (spot it?)
na.omit() on a whole table is a silent decision about every column, including the ones you never look at.
| Language | Style guide | Check | Fix |
|---|---|---|---|
| R | tidyverse style guide | lintr::lint("R/02-clean.R") |
styler::style_file("R/02-clean.R") |
| Python | PEP 8 | ruff check python/ |
ruff format python/ |
STYLE.md: the guide you follow, plus your own rules (“never na.omit() a whole table”)lintr would have found na.rm = T (write TRUE: T is a variable anyone can overwrite).
⏱ 20 min
The example repo was drafted with an AI assistant and reviewed by me. It renders without errors. It also breaks several of our ten habits, and its own STYLE.md.
In pairs, no AI:
STYLE.md, then R/02-clean.R, R/03-reshape-join.R, R/04-describe.R, python/summarize.pyRunning lintr::lint() is allowed. It’s a tool, not an AI, and it finds some of them (not the important one).
Collect the class list on the board. Then:
lintr or ruff have caught? Which needed a person who knew the data?na.omit() one: how many rows, years and countries disappeared? Would you have noticed from the figures alone?Code that runs is not code that is right.
Fixing these is optional practice. Fork the repo and try. You’ll use the same eye on Tuesday.
Most AI coding tools read a project file before every task:
| Tool | Reads |
|---|---|
| Many tools (Codex, Cursor, Copilot agents, …) | AGENTS.md |
| Claude Code | CLAUDE.md (the example’s CLAUDE.md only says @AGENTS.md) |
| GitHub Copilot | .github/copilot-instructions.md |
What goes in it, in the example repo’s AGENTS.md:
STYLE.md, explain each changeFrom the example’s AGENTS.md:
## Boundaries: never do these
- Never modify, rename, or delete anything in `data/raw/`.
- Never add an R or Python package without asking first.
- Never use absolute paths, `setwd()`, or `rm(list = ls())`.
- Never download data or call external services.
- Never edit the "AI use" section of `run-analysis.qmd`; the humans write it.
- Never change results silently: if a change alters a number,
a row count, or a figure, say so.Your boundaries should include the course AI policy: it may help you work, it may not do your understanding for you. “Don’t write the interpretation of results” is a fair boundary for this class.
Long and vague
I’m working on an R project for my stats class about library visits. We have survey data in two waves and we joined them with sites, and I use dplyr and ggplot. Please use tidyverse style and don’t use absolute paths. Can you look at my code and make it better and fix any problems? Also make the plots nicer.
Short and specific (the context is already in AGENTS.md + STYLE.md)
In
R/04-describe.R, the threesummarize()blocks repeat. Replace them with oneacross(). Show me the diff; don’t change anything else.
Stable context goes in files, written once. Each prompt: one task, one file, the check you want. Ask for a diff, not a rewrite. Read it before you accept it.
AGENTS.md and STYLE.md⏱ 10 min
AGENTS.md and STYLE.md from the example repo into your projectThese files are part of Tuesday’s review. Using AI on your project after today? Note in your “AI use” section that it worked under your AGENTS.md.
⏱ 15 min
Groups of 3-4. Pick one question, then share with the class. There’s no right answer; bring a view and a reason.
Ed thread “AI ethics”: one post per group, your question, and the strongest disagreement you had.
STYLE.md: your rules, written once, for humans and AIAGENTS.md: what the project is, what the AI must never do, how it should work, how to checkTuesday, Nov 24 (lecture only, no DS section; DS 9 is on Canvas, due Wed Nov 25): peer code review, in class. Bring your laptop, charged, with your reviewer’s repository cloned if it’s posted early.

STAT 266A · Fall 2026 · UC Santa Cruz