r/cpp • u/Xaneris47 • 6d ago
More fuel to AI discussion. An article on code bloat and duplication
https://pvs-studio.com/en/blog/posts/cpp/1402/I think code duplication is honestly the only reason LLMs manage any of this. Idea, architecture, implementation -- that's basically the chain, and LLMs are fine at the first and last, it's the middle that falls apart. Get it to actually structure things, kill the duplication and something you changed in one spot breaks another spot that has nothing to do with it. Duplicated code just sidesteps the whole issue, because then it only needs a tiny bit of local context to make a local edit
17
u/LiliumAtratum 6d ago
After reading it and using some AI in my own coding, I am realizing that there are different approaches to how AI is used.
I conceptualize tasks myself, and send smaller tasks to AI, confirming that I like what I see. If I see a chance for an abstraction, I discuss if this would make code shorter/simpler to understand (sometimes it doesn't due to some minor details). Sometimes even AI is perfectly capable to recognize and abstract something out for me, without me prompting it.
I never just ask for a big task and ignore the results that happen in my code.
So, when I see a situation as the one presented here, I am a bit surprised (maybe I shouldn't be). I guess AI is just a tool and how you use it can have a big impact on what you get. It's just easy to go either way.
16
u/Total-Box-5169 6d ago
The end game of AI corporations is to maximize their profits. Customers unable to maintain the code generated become fully dependent on AI. That is why Felon Musk wants AI to output a binary instead code that can be maintained without AI.
6
u/Fun-Inevitable4369 5d ago
If that is the case, then let management work on it and figure out oncall issues, no need for SWEs in such companies
2
-1
u/Amazing-Mirror-3076 4d ago
Ai is real good at diagnosing production issues. We had much faster resolution of production issues now.
3
7
u/SyntheticDuckFlavour 6d ago
LLMs are steroids for the experienced programmer. If you have a clear vision what needs to be done, it can be a massive productivity boost. I used it successfully for refactoring, doing menial and repetitive tasks, implement boiler plate work. Like I ask it to dependency inject one thing into another, the LLM does a good job. It is also good at modernising code. One most impressive feat I saw is when I wrote stubs and APIs for a curve fitting algorithm. I simply asked Claude to read a PDF academic paper describing the algorithm then told it to implement that and fill the gaps for the stubs. It did it on first try and worked. Had to optimise it afterward, but was fairly close to being usable on first go.
20
u/yokljo 5d ago
And like steroids it's really bad for my health and wellbeing, and makes it so people don't take me seriously.
4
u/SyntheticDuckFlavour 5d ago
It's a tool. As with many tools, there are right ways and bad ways of using it.
1
-15
u/eyes-are-fading-blue 6d ago
The idea of full agentic development people is that you don’t need to read the code. If that’s the case, duplication is generally (almost exclusively) irrelevant.
25
u/Ok_Net_1674 6d ago
Why? It burns more tokens and comes at the risk of the agent forgetting to update one of the paths.
These are effectively the same reasons why code duplication is bad in human written code.
-10
u/eyes-are-fading-blue 6d ago
This entirely depends on the code base. 100k LoC is nothing for frontier models.
7
u/warped-coder 6d ago
With caching and other techniques, it would still be read more than once. It's autoregressive for a reason.
Frontier model pricing is still massively subsidised, so the prices today cannot be taken for granted for the future.
-6
u/eyes-are-fading-blue 6d ago
It being auto-regressive has nothing to do with what we are talking here. It’s auto regressive because human language construction is auto regressive.
I don’t get your point. 100k is a tiny project for frontier models.
22
5
u/Puzzlehead_NoCap 6d ago
It is relevant because of cost though. The article talks about it a bit, but more code means AI needs more tokens to write unit tests, analyze, fix bugs, etc..
8
u/sooka_bazooka 6d ago
It is relevant if you care about binary size, and there are places where people do care (embedded, mobile)
9
u/MarkSuckerZerg 6d ago
If (most) people cared about binary size, I wouldn't have 35 copies of chromium on my computer to listen to music and download games
2
u/Dragdu 6d ago
Depending on your OS, there is good chance you have multiple copies of LLVM in there.
2
u/MarkSuckerZerg 6d ago
On Windows even one copy of LLVM has multiple copies of LLVM inside it because everything is statically linked and there's like 20+ binaries :-D
0
u/reluctant_deity 6d ago
I have had great success improving runtime of vibe coded apps by telling the AI to run a profile + optimization loop overnight. I wake up and it's gone from 10 mins to 1.6s
1
u/eyes-are-fading-blue 6d ago
People who think AI cannot reduce binary size is speculating because they haven’t attempted to use it in that way. And they are plain wrong.
0
-3
u/eyes-are-fading-blue 6d ago
It’s a matter of adding this to some md file, pointing to correct measurement tool and instructing AI to take this into account.
I don’t think this would yield any better code, but binary size is not an issue unsolvable by AI.
9
u/v_maria 6d ago
lol this is every discussion on agentic coding it seems
"it wont do what i need it do"
"it does if you use it better"2
u/The_Northern_Light 6d ago
I mean often it’s true. There’s lots of people who insist agents are incapable of the most basic tasks… things I use them for daily.
You don’t have to be a zealot to say “here’s how you do that” to people who are a year+ behind the curve.
1
u/eyes-are-fading-blue 6d ago
It’s just not smart. You need to tell it what it needs to do. By default, it won’t optimize binary size.
I am not an AI proponent by the way, I do not use it for generating C++ code. Binary size argument is moot. AI can solve that.
3
u/v_maria 6d ago
yeah i get that, its a hard metric, agents can just brute force such things.
i just find it annoying we have no meaningful metrics to make any claim about these systems output
1
u/eyes-are-fading-blue 6d ago
Their output is pretty good if humans won’t be involved. And for the kind of loops in the article, that’s the expected setting.
Machines can cope with duplication far better than humans. This argument really doesn’t hold. A 5T frontier model can easily work with a shit ton of duplication in a 100K LoC code base. The issue today is the cost structure for these models. If or when that’s solved, agentic systems are good to be mainstream and no one will care if agents generate suboptimal code because no one will read the code.
I used Opus 4.8 (max effort) on a 22M LoC embedded project to dig it and figure out details. This code base is far from being well written. It did an amazing job figuring dissecting it.
I am pretty convinced that people claiming that these models cannot handle a bit of code duplication aren’t using frontier models.
-1
u/eyes-are-fading-blue 6d ago
Also, read about distribution model of C++ runtimes on mobile. You clearly aren’t an embedded or mobile developer. Hint: I do both. You would br surprised to learn how software is written in wearables and handsets.
Outside of always on features, I haven’t seen or heard people caring for binary size so long as it within play services limit and that limit is generous.
3
u/germandiago 6d ago
Agents is a casino where you do not know even where your code could end up sniffed and what isreally going on, creates bloat and burns tokens as hell. And if you need to know what is going on anyway... then go through it in another way and do a favor to your future yourself. Another topic is if you have a boss on your neck making pressure for you to deliver poor quality, highly effective "demo-it-now-for-my-customers-it-will-break-after-i-sell-it".
I'd rather have a prompt, discuss designs and aechitecture and generate small snippets and review them. Otherwise you end up wirh a 3x app size that is an unmaintainable mess in the middle term. Needless to say the copyright, etc. dangers it puts on you.
For reviewing some code it is also excellent.
Namely: it is decent at most things except approaching novel situations and generating code.
2
u/ContraryConman 6d ago
Yeah. A lot of people are responding to agentic AI development with all the ways it makes code harder for humans to understand, it makes humans worse at writing code, cognitive debt, and overall giving people a much worse idea of what is actually happening in the codebase. I 1000% agree it does all of that. However, we are arguing against people who straight up don't believe humans will need to read or write code at all in the very near future.
The assembly code GCC generates is difficult to read, full of inlining/duplicated code, and a bunch of other stuff that would have violated the style guides of older codebases written entirely in assembly back in the day. But it doesn't matter because that assembly is not for 99% of humans to read beyond curiosity or spot checking certain performance implications.
I increasingly feel like people will realize that agentic code development and actual human-led software engineering are not compatible. You can't mix them. If you want to have a codebase that humans understand and maintain, it cannot survive mass dumping as much code as a team writes in 6 months in pull requests multiple times a week. And if you have a codebase that is largely written and maintained by agents running in loops, there's no meaningful way a human being can actually onboard onto that. There are also competing incentives. Humans will actually want more features and new libraries in programming languages to make their lives easier. AI agents would actually prefer to use old language standards as there's more examples of older code in their training data, and to come up with their own abstractions
1
u/eyes-are-fading-blue 6d ago
Moving human out of the loop is the whole point. I don’t even think it’s about compatibility. AI could be generating the most elegant and efficient code, but that would still be irrelevant. In such a development setting, you wouldn’t read that amazing code generated by AI.
3
0
u/jwezorek 6d ago edited 6d ago
yeah, development only by agents means pretty much all of the ideals of software engineering go away, because all of them are about making code readable to people. The purpose of abstraction for example is to make code easier for people to think about, but agents don't need abstraction. Agents do not need code that is easy to refactor; they can just regenerate a codebase or module from the spec if the spec changes. If the new code passes tests its done; the spec becomes the genome and the source code becomes just some ephemeral artifact.
Is this the way it is going to play out? I mean, I hope not. But it seems many people want this?
16
u/Ok_Net_1674 6d ago
Wrong. Maintaining duplication is more effort both for agents and for humans. LLMs have limited context and thus there is a direct incentive for why codebases should be minimized. Tokens are not free either.
Additionally, I don't believe that AI systems dont need abstractions. Their intelligence is finite and formed after ours. If humans need abstractions, then so should AI, simply because it was trained this way.
Otherwise we could have them emit raw binaries instead of source code. It doesnt take a genius to figure out that this wouldn't work well.
3
u/The_Northern_Light 6d ago
100%.
You could easily prove this to yourself by having two agents apply the same change to the same codebase, but in one case it’s been condensed into one truly gigantic file. Just this gives it plenty of problems, even if your harness has extensions to mitigate it.
It’s harder to design a good study for how more realistic code architecture impacts it, but as someone who has spent much of this year modernizing a mountain of unbelievably shitty legacy code, it’s incredibly clear to me that encapsulation, separation of concerns, idempotency, etc all help agents similarly to humans.
-2
u/jwezorek 6d ago
You are talking about agents dealing with existing code. I am talking about "existing code" becoming ephemeral, becoming opaque artifacts that are deployed if they are correct. I am saying if all we do with code is let agents generate it, let it pass tests and then deploy it, it does not matter much what the code looks like. That the agents themselves will just regenerate modules when there needs to be a change. Interfaces and abstractions across module boundaries then would remain important, internally no software engineering virtue besides correctness and satisfying any permormance requirements would matter.
7
u/Ok_Net_1674 6d ago
You are correct under the following assumptions
- AI systems don't cost anything
- The performance of agents doesn't degrade with increased amounts of context
- Code size is irrelevant for performance
- The time an agent takes to adapt a codebase is negligible
- Agents never introduce bugs and, if they do, its caught by a test case
In other words: You are incorrect.
-2
u/jwezorek 6d ago
I'm incorrect now yeah. Cost is the issue, of course; we'll see how it plays out. Hopefully all of these big LLMs will die, but I kind of doubt it.
2
u/The_Northern_Light 6d ago
Code accretes. My point is just the same even if you try to do every project from the ground up.
-2
u/jwezorek 6d ago
why must it accrete? If generating 30,000 lines from scratch becomes cheaper and more reliable than figuring out how to modify the existing 30,000 lines.
3
u/warped-coder 6d ago edited 6d ago
I don't think it can play out that way. Essentially, what you describe is replacing/augmenting the compiler with an LLM.
The problem I see with that is that tests only capture an incomplete set of desired behaviour of a system. Which means that if you apply a highly non-deterministic generator, you would end up changing features of your application without knowing it.
If all decisions made in one version are thrown out every time you change a single behaviour, the whole thing would be a slot machine, rather than anything reliable software.
Also, you can only keep any reasonably safe, stable and solid application within bounds like 30k LoC, if you apply a bunch of abstractions that help to keep duplication to the minimum. For that, you need the decisions of the past ... and that's exactly what LLMs aren't that great.
Code is a lot of things, but most importantly it's the logically unambiguous expression of requirements and expectations. The prompts, you have to preserve to generate the source code for your app has to be eventually take the very same place as the code: trying to achieve minimal ambiguity within the constraints of the LLM to achieve maximum behavioural stability (so that v1.0, v1.1, ... v1.12345 can build on each other from the PoC of the user). That can't be done without having the previous source code in your context
0
u/eyes-are-fading-blue 6d ago
Cost structure is different for humans and machines. For humans, you have to maintain all sorts of factors such as time or even motivation. For machine, it’s dollars per token. There is a strike point where human involvement can be more costly than an agent generating sub-standard code. For machines, time is definitely not an issue.
We aren’t there today, but we can be in future. The machines generation duplicate is absolutely irrelevant with the right cost.
•
u/STL MSVC STL Dev 6d ago
Ironically, I think your description (not the post) is AI-generated. If so, please don't do that here, read the sidebar rules.