Lesson 2 of 4 · Free to read
Exercises
Exercises — Context, APIs, and AI IDE Debugging
Watch & learn
Course video
Course video
Keep a prompt log while you work. Speed without a trail is not a skill — it is luck.
How to use this file
- Work in pairs: one drives (edits), one reviews (diff + evidence log).
- Every exercise ends with a testable success criterion.
- Keep an
EVIDENCE.md: hypothesis → experiment → result.
Exercise 1 — Hypothesis-driven debugging log
Goal: Debug a failure without guessing, using AI to propose hypotheses and tests. Time: 35 minutes Level: foundational Modules touched: [20-context-and-apis-for-juniors, 08-testing-and-quality]
Setup
- Pick any small bug in a sandbox project (or create one intentionally).
- Create
EVIDENCE.mdwith headings: Symptom, Hypotheses, Experiments, Result.
Steps
- Write the symptom (copy the exact error / failing assertion).
- Ask AI for 3 hypotheses and 1 experiment per hypothesis.
- Choose one hypothesis and run the experiment.
- Apply the smallest fix that makes the failure disappear.
Success criteria
- You can show: (a) the failure, (b) the change you made, (c) the verification (test or repeatable steps).
AI prompt hints
- "Ask Cursor: 'Here is the failing test and error. Propose 3 hypotheses and the minimal experiment to validate each.'"
- "Ask Cursor: 'Suggest the smallest code change that would validate hypothesis #2, without refactoring.'"
Solution sketch
AI is best at proposing hypotheses. You keep control by choosing experiments and requiring verification before moving on.
Stretch
- Add a regression test that fails before the fix and passes after.
Exercise 2 — Create a local API stub and integrate it
Goal: Integrate an API without paid services, using a local stub endpoint. Time: 45 minutes Level: intermediate Modules touched: [20-context-and-apis-for-juniors]
Setup
- Create a tiny HTTP server (Node or Python) that returns JSON.
Steps
- Implement
GET /weather?city=...returning{ city, tempC }(fake values ok). - In your app, call this endpoint with a timeout.
- Add handling for a non-200 status code.
Success criteria
- When the stub runs, your app displays the returned JSON values; when the stub returns 500, your app shows a clear error message.
AI prompt hints
- "Ask Cursor: 'Create a minimal Node or Python HTTP stub with /weather?city= that returns JSON and supports returning 500 for city=fail.'"
- "Ask Cursor: 'Add a fetch call with a 2s timeout and explicit handling for non-200 responses.'"
Solution sketch
Stubs let you practice contracts and error handling without external dependencies. Timeouts and status handling are non-negotiable.
Stretch
- Add a simple retry (one retry after 200ms) only for network errors.
Exercise 3 — Secrets and env vars (no leaks)
Goal: Move a “secret” out of code and into environment variables. Time: 25 minutes Level: foundational Modules touched: [20-context-and-apis-for-juniors, 15-security-and-supply-chain]
Setup
- Add a fake secret:
API_KEY="dev-123"(do NOT commit real secrets).
Steps
- Remove the key from code and read it from an env var.
- Make the app fail fast with a helpful error if the env var is missing.
- Add
.env.example(if not present) documenting required vars.
Success criteria
- The app starts only if the env var exists;
git diffshows no secrets added.
AI prompt hints
- "Ask Cursor: 'Refactor this code to read API_KEY from environment and throw a clear error if missing.'"
- "Ask Cursor: 'Add an .env.example and update README run instructions.'"
Solution sketch
Secrets in code are effectively public. Failing fast avoids confusing runtime errors later.
Stretch
- Add a check that redacts the key if it appears in logs.
Exercise 4 — Contract check for API responses
Goal: Add a small runtime validation that prevents “undefined crashes” from bad API responses. Time: 40 minutes Level: intermediate Modules touched: [20-context-and-apis-for-juniors, 08-testing-and-quality]
Setup
- Modify your stub to sometimes return
{ city: null }or omittempC.
Steps
- Add a runtime guard: required fields exist and have the right type.
- When validation fails, show a user-friendly error without leaking raw response.
- Add a unit test for the validator.
Success criteria
- Bad responses do not crash the app; tests prove the validator rejects invalid shapes.
AI prompt hints
- "Ask Cursor: 'Write a runtime type guard / validator for this JSON shape and add tests.'"
- "Ask Cursor: 'Update the UI to handle validation failures with a clear message.'"
Solution sketch
External systems fail in weird ways. Runtime validation is cheap and stops the failure at the boundary.
Stretch
- Add a “safe parse” that records a debug-only error without exposing it to users.
Exercise 5 — PR + AI-assisted review note
Goal: Open a PR that documents what AI suggested and what you accepted/rejected. Time: 35 minutes Level: advanced Modules touched: [20-context-and-apis-for-juniors, 07-version-control-and-collaboration]
Setup
- Ensure your branch has at least one failing test that you fix, or a feature with tests.
Steps
- Write a PR description: problem, approach, tests, screenshots (if UI).
- Add a section “AI assistance” with:
- prompts used (2–4 bullets)
- accepted suggestions
- rejected suggestions + why
- Confirm tests are green.
Success criteria
- PR description includes an “AI assistance” section and a green test run.
AI prompt hints
- "Ask Cursor: 'Draft a PR description for this change. Include tests run and an AI assistance section.'"
- "Ask Cursor: 'Given this diff, identify risky parts and add a review checklist.'"
Solution sketch
The goal is not to hide AI usage; it is to show judgement and verification. Rejected suggestions are often the most valuable signal.
Stretch
- Add a simple changelog note for the user-visible behavior.