debugging mistakes lessons

When AI Gets It Wrong: A Debugging Story

AI isn't perfect. Here's how to handle it when your AI assistant produces buggy, wrong, or just weird code.

· VibeWerks

AI is incredible. AI is also wrong sometimes. Here’s what to do about it.

The lessons:

  • “Handle edge cases” is too vague — be specific
  • Test immediately — before shipping
  • AI writes happy path code — you add defensive code
  • First draft ≠ production code — review everything

The Setup

I asked Claude to create a simple function:

Create a function that calculates the average of an array of numbers.
Handle edge cases.

It gave me:

function average(numbers) {
  return numbers.reduce((a, b) => a + b, 0) / numbers.length;
}

Looks good, right? I shipped it.

The Bug

A week later, a user reported a crash. The error: Cannot read property 'reduce' of undefined.

What happened? Someone passed null instead of an array. The “handle edge cases” instruction wasn’t specific enough.

The Fix

I went back to the AI:

This function crashes when numbers is null or undefined.
Also fails on empty arrays (divides by zero).

Fix it to:
- Return 0 for null/undefined/empty
- Only accept arrays of numbers
- Throw clear error for invalid input

New version:

function average(numbers) {
  if (!Array.isArray(numbers) || numbers.length === 0) {
    return 0;
  }
  
  const validNumbers = numbers.filter(n => typeof n === 'number' && !isNaN(n));
  
  if (validNumbers.length === 0) {
    return 0;
  }
  
  return validNumbers.reduce((a, b) => a + b, 0) / validNumbers.length;
}

Much better.

Lessons Learned

1. “Handle edge cases” is too vague

AI doesn’t know which edge cases matter to you. Be specific:

❌ “Handle edge cases” ✅ “Handle: null input, empty array, non-number values, NaN”

2. Test immediately

Before shipping, I should have run:

average([1, 2, 3])     // ✓ works
average([])            // ✗ returns NaN
average(null)          // ✗ crashes
average([1, 'two', 3]) // ✗ returns NaN

Would have caught it in 30 seconds.

3. AI assumes happy path

Unless you tell it otherwise, AI writes code for the ideal scenario. Real users don’t follow ideal scenarios.

4. First draft ≠ production code

AI code is a starting point. Your job is to:

  • Review critically
  • Add edge case handling
  • Write tests
  • Then ship

Common AI Mistakes

MistakeWhy It HappensPrevention
Missing null checksAI assumes valid inputSpecify input constraints
Wrong importsAI doesn’t see your projectProvide file context
Outdated syntaxTraining data cutoffSpecify versions
Security issuesAI optimizes for function, not securityAsk for security review
Wrong librarySimilar names, different libsSpecify exact library

The Right Mindset

AI is a junior developer who:

  • Writes fast
  • Knows lots of syntax
  • Follows instructions literally
  • Doesn’t understand your business logic
  • Needs code review

You’re the senior developer who:

  • Reviews everything
  • Knows the edge cases
  • Understands the context
  • Signs off before shipping

This isn’t a flaw in AI. It’s just how the collaboration works.

Red Flags to Watch For

When reviewing AI code, look for:

  • No error handling — What happens when things fail?
  • Hardcoded values — Should these be configurable?
  • Missing validation — Is input being checked?
  • Assumed availability — What if that API/file/service is down?
  • Silent failures — Are errors being swallowed?

If you see these, ask the AI to fix them specifically.

The Meta-Lesson

The bug wasn’t the AI’s fault. It was mine. I:

  • Gave vague instructions
  • Didn’t test
  • Shipped without review

AI is a tool. Tools don’t make mistakes—people using tools make mistakes.

Get better at using the tool, and you’ll have fewer bugs.


🎯 Apply This Now

Next time you get AI-generated code:

  1. Before accepting: Read every line
  2. Ask: “What inputs could break this?”
  3. Test: Run it with null, empty, and weird inputs
  4. Fix: Ask AI to handle the edge cases you found

One extra minute of review saves hours of debugging later.


Guides:

Cheatsheets:

Blog: