Debugging AI-generated code
Understand the idea
A generated helper looks polished and passes its example, but an empty returns . Plausible structure is not evidence that the contract is correct.
A proposal is not evidence
Generated code can be readable, neatly commented, and still make the wrong assumption. Formatting tells you how easy it is to inspect; it does not show that the behavior matches your application. The same review discipline is useful for unfamiliar code written by anyone.
State the contract first
Fluent comments, conventional names, and familiar s can make a solution feel complete. They do not prove it handles your runtime, dependency versions, data shapes, or failure cases. A model can reproduce a common-looking pattern while missing the condition that matters in your application.
Write down what should happen for ordinary input and for a meaningful boundary. For this lesson, the agreed contract says the average of an empty list is zero. That is an application choice, not a mathematical law. Choosing the expected result before inspecting the implementation keeps the code from defining its own success.
| Claim to verify | Small useful check | Why appearance is insufficient |
|---|---|---|
| Handles empty input | Run the explicitly agreed empty case | A normal example never executes the boundary |
| Uses a real API | Check the installed API’s name and return contract | A plausible method name may be invented |
| Handles failure | Cause a rejected request or invalid input | A catch can silently suppress the failure |
| Keeps data independent | Change a copy and inspect the original | A spread may still share nested objects |
| Fits the environment | Run in the target module/runtime setup | Browser and server globals differ |
Choose a counterexample
Before running the code, write down its inputs, promised outputs, , and unacceptable behavior. Check imported APIs against the actual installed package and its documentation. Inspect any network, file, authentication, or data-deletion behavior before giving it real credentials or production data.
A counterexample is one input that disproves a claimed behavior. Empty , zero, missing fields, duplicate values, and rejected requests often expose assumptions hidden by a happy-path demonstration. Pick boundaries from the actual contract instead of throwing arbitrary strange inputs at every .
Verify the environment and APIs
Use counterexamples deliberately: empty input, one item, zero, negative values, missing fields, duplicates, large input, and a rejected async operation where relevant. Do not ask the same generated explanation to certify itself. Compare results with independently chosen expectations and existing tests.
Repair one cause and rerun the cases
When a case fails, reduce it and fix the specific assumption. Add that case to the tests, then inspect whether the repair changes other requirements. For this lesson, the contract chooses zero for an empty collection; a statistics library might instead reject it. The right behavior comes from the contract, not from whichever code looks most confident.
Check that suggested APIs exist in the installed environment and that their returned values have the expected shape. Run the smallest failing case before the repair, then run both that case and the previously working case afterward. If a generated catch block returns a success-shaped fallback for every failure, inspect whether it hides lost work rather than recovering from it.
Mental model
FOLLOW THE VALUE
Choose behavior before judging the code.
01 / 03Trace the transformation
Live example
The empty is the counterexample. A guard handles the explicit contract before division, while normal and zero-valued inputs retain their behavior.
Your console output appears here.
Guided challenge
Repair the empty-input contract
Make the guard return zero for an empty array without changing non-empty averages.
Your console output appears here.
EXPECTED CONSOLE OUTPUT
3 0 0
Hints 0/2
Use all hints or make 3 failed runs (0/3).
Free sandbox
Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.
Your console output appears here.
Quiz
A quick check for understanding. Retry as often as you like; your learning path stays open.
Ready to call this one understood?
Mark this lesson complete to earn 200 XP. You can always revisit it.