Skip to content
engineering workbench.by StudyPort Academy
Learning path
Intermediate22 minLESSON 03 / 6

Why AI code looks right - and isn't

01

Understand the idea

Syntactically clean code can still encode the wrong rule. AI often optimizes for looking finished, not for being correct on your contract.

The track's central claim

This is the thesis of the track, not a side note: AI-generated code is often syntactically clean, conventionally named, and easy to skim - and still wrong for your problem. The model is rewarded for producing something that looks like working software. Your job is correctness against a contract the model does not own.

That gap is not rare bad luck. Teams shipping with assistants keep rediscovering the same shape of failure: a helper that passes the demo input, a catch that silences an error, a string check that almost means equality. The code reads like a junior engineer who memorized patterns and skipped the product rule.

AI Lab · spot the flaw

AI-generated login lookup that "works" on the demo email

Asked for a helper that finds a user by email typed into a login form. The model returned clean, familiar Array code. The demo email "ada@example.com" works. Something subtler is wrong.

javascript

function findUserByEmail(users, query) {
  const normalized = String(query).trim().toLowerCase();
  return users.find((user) =>
    user.email.toLowerCase().includes(normalized)
  );
}

// Demo that persuaded the author:
const users = [
  { id: 1, email: "ada@example.com" },
  { id: 2, email: "ada.lovelace@example.com" },
  { id: 3, email: "grace@example.com" },
];
findUserByEmail(users, "ada@example.com"); // -> user 1, looks fine
What is actually wrong?

Why plausible code wins the glance test

Familiar APIs reassure you. includes, try/catch, map, and "safe" defaults are patterns you have seen in good codebases. Glance review checks shape, not intent. If you stop at shape, you rubber-stamp a of correctness - not always an invented API, often an invented rule.

What the glance test misses
Looks fineHidden missPressure test
email.includes(query)Substring identityQuery ada against two Ada addresses
catch { return 0 }Silent wrong totalMissing catalog id
await in a tight loop mythStill one JS threadBusy loop after await
Comment says continueBreak still abortsBatch with two bad files

What clean snippets commonly miss

Watch for three recurring misses. First, the demo never ran: empty input, partial matches, duplicates, slow networks. Second, security and auth boundaries stated softly in prose and implemented as convenience checks. Third, intent drift: the code solves a neighboring problem ("find a string") instead of yours ("authenticate this exact account").

None of those misses require exotic bugs. They require a model that finishes the sentence that usually appears after "users.find".

Pressure-test with your contract

Write the rule in one sentence before you read the snippet. "Login email must match exactly after lowercasing." Then choose an input that would fool a softer rule. Run that input. If the snippet fails, you found the real review. If it passes, pick a second boundary. Do not ask the same model to certify its own patch without an independent expectation.

Build a habit before you trust the patch

Treat every generated change as untrusted until a contract check says otherwise. That habit is the whole point of AI Lab in this track: judgment under realistic conditions, not another page of tips. Looking right is the model's strength. Deciding whether it is right is yours.

02

Mental model

FOLLOW THE VALUE

glance
familiar helpers
false confidence

includes looks busy and correct.

01 / 03Trace the transformation

03

Live example

The demo shows a substring email lookup that looks ordinary and still encodes the wrong identity rule.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022
04

Guided challenge

Name the real flaw

The third log says exact. Change it to substring to name what the helper actually does.

Opening editor…
LIVE PREVIEW
Console 0

Your console output appears here.

Saved on this deviceJavaScript · ES2022

EXPECTED CONSOLE OUTPUT

ada
ada@example.com
substring
not-exact
review-me

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.

    01Why can AI-generated code look right while being wrong?
    02What should you reach for after a happy-path demo passes?

    Ready to call this one understood?

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

    Sign in to save progress
    Next lesson