T
22

Just figured out a trick for getting AI to write better code comments

I was stuck trying to understand some old code I wrote that had zero comments. So I used an AI tool to generate them, but it kept spitting out vague, useless notes. After a bit of trial and error, I found that breaking the code into small sections and asking for plain English explanations for each part made the AI give much clearer results. Now my code is way easier to follow, and it saved me hours of headache.
4 comments

Log in to join the discussion

Log In
4 Comments
john_sullivan2
Which specific AI tool were you using for this? I read something a while back about how these tools often just parrot back the code structure unless you force them to think about the actual goal. Like, telling it "explain why this sorting function is here" instead of "describe this function." Makes it connect the code to the bigger task. I wonder if some tools are just worse at this by default.
7
wells.seth
Chunks force it to see context.
3
maxpalmer
maxpalmer7h ago
Wait, you actually got an AI to write good comments? That's wild, because every time I've tried, it just described the code in a super obvious way (like "this loop iterates through the array"). Breaking it into chunks is such a simple fix, but I never would have thought of it.
2
elizabeththomas
Try asking it why the code works instead of just what it does. That forces the AI to explain the purpose behind each chunk, not just repeat the syntax. What kind of code were you trying to get comments for anyway?
7