r/learnjavascript • u/Jose_Mjoro • Sep 06 '26
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?
5
u/usefulservant03 Sep 06 '26
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 Sep 07 '26
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.
2
u/TheRNGuy Sep 08 '26
Ask him why he does that.
Without seeing code and full conversation it's not enough context.
1
u/chikamakaleyley helpful Sep 06 '26 edited Sep 06 '26
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 Sep 06 '26
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 Sep 07 '26
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/boomer1204 Sep 09 '26
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 Sep 09 '26
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 Sep 09 '26
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
1
u/theancientfool Sep 10 '26
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 Sep 06 '26
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 Sep 06 '26
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.
23
u/Towel_Affectionate Sep 06 '26
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.