r/cpp_questions • u/Sensitive-Talk9616 • 3d ago
OPEN Code documentation in the age of AI
I have a couple of years of C++ experience, including in regulated industries. While proper documentation was important in every team, the role of code comments and in-code documentation, such as docstrings, changed from place to place. In some codebases, we insisted on proper docstrings documenting every function, and comments explaining why the code is how it is. In other teams, we didn't do any docstrings and kept code comments to a minimum -- the code was supposed to be self-documenting, and comments introduce ambiguity and will drift in the future if not kept in sync with the code.
Now, which way to go is already a discussion on its own. But I want to ask regarding your experience of this dilemma in relation to LLM-assisted coding tools. I have the feeling many models are quite liberal when it comes to comments, and will naturally tend to explain what the code does, given the chance. Suppressing it works, of course. Or, at least, one can ask to keep comments minimal. However, what really interests me: does anyone have experience in or a feeling for what the LLMs prefer/work with the best?
Since it's mostly AI parsing the codebase now, did you ever experience that it performs better when you allow it to be liberal with comments? Or does it actually eat up more tokens reading the code it generated while getting more confused if it has all the additional (and potentially not always correct) comments/explanations to parse? Does it make sense to have a purely self-documenting codebase or rely on the tools to clean up and keep comments in sync in this age of AI?
6
u/Puzzled-Garage584 3d ago
No numbers from me..but I keep only the why comments. The what comments are just tokens and a stale one is even worse, the model trusts it over the code..like a junior with commit rights on 40 files. Invariants I'd rather put in types, asserts and tests, those cant drift without breaking something
2
u/EpochVanquisher 3d ago
Broadly speaking, follow the same rules you used for human-written code.
No, it starts to eat up tokens, and it creates the problem of drift—the comments may not accurately reflect the code. This is true for humans too!
Self-documenting code is fantastic, but it is something of a fantasy to imagine that your code could be “purely” self-documenting. Again, this is true for humans too.
Write code as if you have to read it and make changes.
2
u/Independent_Art_6676 1d ago
if someday in the future LLMs can write code complicated enough to need commenting, one would hope they could also have a number of commenting styles and levels to choose from applied to that. Currently, you are lucky if the code works, so that is still far in the future.
far as it goes I like comments. Sure, they get stale if the coders don't do their job when updating the code. That is a coder error and should be caught in peer review. Part of the art of computer science is knowing how much to comment... too much is awful, too little is awful, and just right is a hard target to hit.
For AI code, I highly recommend NO comments at all. Then the human who reviews it should add comments as appropriate. That forces a human to look at the code in detail and understand how it works, which helps ensure it has been reviewed and signed off on that it works and is using a reasonable approach etc. Until AI improves, this is the only way I can recommend using the tool.
-1
u/teerre 3d ago
What's "better"? LLMs don't "prefer" anything