Skip to content
engineering workbench.by StudyPort Academy
Learning path
Real-World20 minLESSON 32 / 34

Debugging AI-generated code

01

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.

Turn plausible claims into checks
Claim to verifySmall useful checkWhy appearance is insufficient
Handles empty inputRun the explicitly agreed empty caseA normal example never executes the boundary
Uses a real APICheck the installed API’s name and return contractA plausible method name may be invented
Handles failureCause a rejected request or invalid inputA catch can silently suppress the failure
Keeps data independentChange a copy and inspect the originalA spread may still share nested objects
Fits the environmentRun in the target module/runtime setupBrowser 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.

02

Mental model

FOLLOW THE VALUE

proposal
state contract
empty → 0

Choose behavior before judging the code.

01 / 03Trace the transformation

03

Live example

The empty is the counterexample. A guard handles the explicit contract before division, while normal and zero-valued inputs retain their behavior.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Repair the empty-input contract

Make the guard return zero for an empty array without changing non-empty averages.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

3
0
0

Hints 0/2

    Use all hints or make 3 failed runs (0/3).

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    Opening editor…
    LIVE PREVIEW
    Console 0

    Your console output appears here.

    Saved on this deviceJavaScript · ES2022
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01What establishes whether average([]) should return zero?
    02Which verification is independent evidence?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress
    Next lesson