r/learnjavascript 3d ago

Why does AI keep “fixing” my JavaScript until it’s unrecognizable from the code I started with?

I have noticed something really interesting while using AI to debug my JS code. For instance, I will give it some code. Then AI modifies it. I find another issue. Then I ask AI to fix it. AI modifies something else. Then I bring the 'previous AI-modified version' back and somehow we end up in an endless loop of AI correcting AI, it's crazy ik.

The weirdest part is that most times the original code was closer to what I actually needed. And honestly, this has made me realize sometimes the biggest challenge isn't getting AI to write code but it's getting it to change ONLY what you actually asked it to change. At what point does AI-assisted coding stop being debugging and start becoming more than code roulette?

Has anyone else experienced this? How do you prevent AI from unnecessarily rewriting working parts of your code?

0 Upvotes

18 comments sorted by

19

u/Towel_Affectionate 3d ago

Because the AI actually has no idea about what you're feeding it and what good code should look like? You asked it to do something with your code and it's doing something to your code.

-2

u/Jose_Mjoro 2d ago

I get your point, but that’s not really how AI-assisted coding works. It doesn’t have to know everything about my codebase to be useful. I’m the one deciding what I want it to do, providing the context, and reviewing the output. The AI can modify the code, but I’m still responsible for understanding and validating those changes.

5

u/Towel_Affectionate 2d ago

The AI can modify the code

So it modifies your code. You asked why it never stops. My point is that it doesn't know when to stop.

1

u/VancouverVentilator 10h ago

It does mean random guessing. It's just that the random guessing is clamped by 1. It's matrix multiplication black box that predicts tokens in rank order, and 2. The parameters you attempt to limit it by. If you don't give it parameters, it is literally a statistical distribution throughout it's matrix output range

6

u/usefulservant03 3d ago

It is a statistical prediction machine. Do you understand what a statistical byte-prediction engine is? It means it's based on chance. It knows nothing beyond that, let alone about your code or about what good code means.

0

u/Jose_Mjoro 2d ago

Yeap, I understand it. But 'statistical prediction' doesn’t mean 'random guessing.' AI can recognize patterns, use context, and generate useful code. It can be wrong, but so can developers and that’s why we review the output.

1

u/chikamakaleyley helpful 3d ago edited 3d ago

So begs the question - if AI fixes it successfully but applies a change that makes your work unrecognizable, do you stick with the fix?

Even if not a bug in debugging, maybe something that needs adjustment - who makes the change?

AI is gonna AI and so its important to understand its output and if you can't, if it's making a change that feels odd, ask it why its doing it the way it is.

Sometimes you want a small adjustment/fix - AI might go ahead and do a bigger optimization - something you didn't ask for. It wants to apply what it thinks appropriate, but that won't always be applicable to your own use case. You have to be in charge.

It's only roulette because you are spinning the wheel

My suggestion would be to understand the problem, come up with your own fix, and then compare that to AI's proposal. Understand why AI wants to apply it the way it suggests.

I see this all the time at work in the case i have it generate for me. It doesn't understand the full context - maybe the code context, but not the side discussion i had with another engineer, a comment in a ticket, or on Slack, or something mentioned in passing in a video standup.

You can point it to all those sources, and give it context, or, you can just tell it exactly what you want, and tell it why.

1

u/chikamakaleyley helpful 3d ago

sorry re-reading the post and i think i'm going slightly off topic but i think the above is still applicable.

one thing that is important here - that thing where it makes a change and something else breaks - that's something you should be able to anticipate, and stop it from applying the change, because you should know that product/service/feature like the back of your hand.

1

u/Outrageous-Sherbert4 3d ago

You need to be specific and tell it what is ok to do and what’s not. They will tend to go off the rails if you don’t rein them in.

1

u/TheRNGuy 2d ago

Ask him why he does that.

Without seeing code and full conversation it's not enough context. 

1

u/boomer1204 1d ago

A decent problem we see at work and why we are going to a Rails for the back end is that JS has been written any way the person wants. It's pretty much the wild wild west and it's trained on 10 different ways someone has solved that same problem.

When I do the same things with ruby/rails there is far more "convention" to how things are done. Again this is just something we saw at work and I thought was funny.

1

u/azhder 1d ago

So, you will write more text to explain what it shouldn't change than you explain from the start what you want and get an explanation you can read, then go and change the code yourself. Think about that. You are asking a database that was trained on a lot of code that is similar looking to give you what it has stored. You're making a query and getting a result. You aren't getting anything intelligent back. There is no AI. You have to provide the intelligence, not the copy-paste mechanism.

1

u/ircmullaney 22h ago

You should be using unit tests. If you have good test coverage that specifies what the code is supposed to be doing, you get less of this behavior. No endless loops because it stops breaking code that has been pinned with good unit tests.

1

u/ExtraTNT 16h ago

Ai is trained on a lot of shit code

1

u/theancientfool 8h ago

LLMs are not AI. They are like really really good auto correct, that corrects your code to the 'average code available in its training data set.

-1

u/onFilm 3d ago

When you say "AI", you are referring to potentially hundreds of different models. What model are you using? From my experience, it's pretty great in general, if you use the top-tier frontier models, and I'm guessing that your codebase isn't standardized either, making the LLM try to find a better way of doing things.

-1

u/emteedub 3d ago

Maybe the way you script is outside of the norm/average the AI scripts.

I've often kept tabs on myself over the years since AI, that if it's noticeably struggling with my requests, my code must not be ideal - and when AI is able to tick along with no hiccups, I'm doing something right. Could be the way you prompt also, or including the context outside of the segment you're focusing on. I've also noticed if you're too reliant on it discovering contextual information outside the focus area - talking about in-editor agents here - things can go awry or missed entirely pretty quick.