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.
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
| Mistake | Why It Happens | Prevention |
|---|---|---|
| Missing null checks | AI assumes valid input | Specify input constraints |
| Wrong imports | AI doesn’t see your project | Provide file context |
| Outdated syntax | Training data cutoff | Specify versions |
| Security issues | AI optimizes for function, not security | Ask for security review |
| Wrong library | Similar names, different libs | Specify 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:
- Before accepting: Read every line
- Ask: “What inputs could break this?”
- Test: Run it with null, empty, and weird inputs
- Fix: Ask AI to handle the edge cases you found
One extra minute of review saves hours of debugging later.
Related Resources
Guides:
- Debugging with AI — Systematic debugging approach
- Testing AI Code — Catch bugs before shipping
- Prompting Fundamentals — Clearer prompts, better code
Cheatsheets:
- AI Debugging Checklist — Step-by-step process
- Common Errors — Quick fixes
Blog:
- 7 Mistakes to Avoid — More lessons learned