How AI code goes wrong

Lesson 22 Scope creep

6 min read · JavaScript · Quiz

unasked rewrites, deleted code, duplicate helpers, needless abstraction

What you'll learn

  • Separate the change you asked for from the extras
  • Question deleted guards, renames and duplicate helpers
  • Ask for the smallest change that does the job

You asked for one small change. The AI sends back a diff that touches the whole file. Every extra line is something you now have to review.

The idea

AI loves to "improve" while it is in a file: it renames things, deletes guards that look unnecessary, adds a second helper that does what the first one did, or wraps a one-line job in a class. Each change might be fine, but none were asked for, and each can break something that was working.

Read a diff with one question: does this line serve the request? Lines that do are the change. Lines that don't are scope creep: ask the AI to take them out, or review them as carefully as new code. A deleted line deserves the most suspicion: someone put it there for a reason.

invoice.js (task: show the coupon)
1 function invoiceLines(order) {2-  const lines = order.lines || [];3-  return lines.map((l) => `${l.name} ${money(l.cents)}`);4+  const lines = order.lines.map((l) => `${l.name} ${formatPrice(l.cents)}`);5+  if (order.coupon) lines.push(`Coupon ${order.coupon}`);6+  return lines;7 }8+9+const formatPrice = (n) => "$" + n.toFixed(2);
  1. Line 2 A deleted guard. It handled orders with no lines. Nobody asked to remove it, and without it those orders throw.
  2. Line 3 The existing helper money was swapped for a new one. Not part of the task.
  3. Line 5 This is the only line the task needed.
  4. Line 9 A second helper that duplicates money, and it prints different text for the same input.

Take away

  • Read a diff with one question: does this line serve the request?
  • Deleted guards and renamed exports are the riskiest extra changes.
  • A duplicate helper is a bug waiting: two versions will drift apart.

Quiz: test yourself

7 questions: a quick quiz, then a review drill. Pick an answer to see why it's right.