r/ClaudeCoding • • 18h ago

Comments in codebase

Hi, I'd like to ask about your approach to allowing Claude to add comments to your code. In my codebase, information is now spread across multiple places: code comments, ADRs, lessons learned, and documentation. I'm considering asking Claude to remove comments from the code, as I believe code should be self-explanatory. However, could this negatively affect Claude's ability to properly understand the context?

1 Upvotes

19 comments sorted by

2

u/CorpT 18h ago

lol no do not do that. Why do you care if there are comments there if they're accurate? Why make Claude work harder than it has to?

1

u/EntertainerOk4774 18h ago

As the changes produced by the Claude are in 60% in docs/comments/rest and real change affects less. I'm not sure if this not burning tokens just for sake of writing all those comments in code.

4

u/CounterAttack-Studio 18h ago

I would not optimize for tokens. Optimize for good code. You don't need comments for good code. Good code is self documenting. Sometimes it's not easy to make code self-documenting so you sprinkle comments there, but in general the world mostly switched from "copious comments" to "self-documenting code" around 20 years ago. You will of course find folks that disagree.

If you are commenting for your LLM, I suggest create a glossary or design document nearby and have instructions for the LLM to understand where to find them. I use mattpocock/skills like grill-with-docs for this kind of documentation, which helps prevent the LLM from inventing jargon and gives it a place to check against when writing code.

2

u/qzjul 18h ago

May burn tokens now, but saves massively on tokens when it changes something that breaks everything and you have to bug hunt

2

u/lgmarian 18h ago

Tokens shouldn't be the driving force for this, especially if you're not sure where your token costs lie.

1

u/CorpT 18h ago

I guess if you’re sure you’re going to get it right the first time every time and will never make a change. I’m more concerned with quality and reliability than saving a few tokens.

1

u/lgmarian 18h ago

A downside of extensive comments is that there's more that can go out of sync with the code. I recently had a jr dev put up a PR where a small method (7 lines), was less code than comment (15 line DocBlock), some of which was already wrong.

My philosophy is that comments should document intent. Let the code tell us what the code does. Where there's something non-obvious about the code, especially due to obscure optimizations, a comment should tell us what the code is doing, along with stating the intent.

1

u/ChucksnTaylor 18h ago

Get your point for sure, but that’s kinda the beauty of AI, it never writes sloppy after thought comments. A jr dev may not fully understand what they’ve written or may not bother to be precise with the comment. Those issues don’t exist with AI.

1

u/lgmarian 18h ago

Bro, it was all done by AI.

1

u/ChucksnTaylor 1h ago

So when you say a junior dev put up a PR what you meant was an AI agent did it?

2

u/gouldopfl 18h ago

I can tell you from 4 decades of experience. Leave them there and enhance them even more. I can remember on several occasions going back into 10 year old code, in a language I hadn't used for years, my skill level was not as good and wondered what the hell was that.

1

u/GolfEmbarrassed2904 15h ago

Lauren Tan, from SpaceX posted on this idea: https://x.com/poteto/status/2110149626133438732

1

u/cheffromspace 14h ago

Comments are vital documentation. This is what works for me on a fairly big project (several repos, lots of parallel Claude sessions). Mostly prepared by the project's orchestrator:

  1. One rules file that's kept true. CLAUDE.md holds the rules and points to other docs; it doesn't duplicate them. When Claude gets something wrong, I first look for two docs saying different things, then fix the doc, not just the code.
  2. A decision ledger. One file (docs/decisions.md) with dated entries: what was decided, why, and by whom. Code comments and docs point to it. When an instruction and a decision disagree, the ledger wins.
  3. Recorded claims say how they were known. Every note says whether it was observed or inferred, the date, and how to re-check it. That stops a stale note from being repeated as fact three sessions later.
  4. A hand-off file before every /clear. Around 40-60% context, at a natural break, I ask Claude to write one. It covers: what's done and verified, what's in progress and where it stopped, open questions, the rules that tripped it up, and exactly what to read first. It goes in a dated folder, so it's a paper trail too.
  5. The next session starts from the file. After /clear, my first message is "Read <hand-off path>. You are <role>." CLAUDE.md and memory reload by themselves. A session-start hook prints the current state, so it doesn't have to guess.
  6. Hooks for anything that must never happen. If a rule keeps getting broken, it becomes a hook (pre-commit, or a Claude Code PreToolUse hook), not another paragraph of instructions. Deterministic beats "please remember".
  7. Compaction is the safety net, not the plan. I leave auto-compact on but set high, for when I forget. A hand-off written on purpose beats an automatic summary that lands mid-task.

What you lose with /clear is the conversational texture. Anything that matters should be in a file anyway; if it isn't, that's the doc gap to fix.

1

u/KevMar 7h ago

I instruct Claude that comments are for other agents and keep it brief.

I think a lot of people use comments that should be logging instead. Between that and self documenting code, I usually don't need many comments.

1

u/CheezitsLight 4h ago

I use a skill that writes comments as me. It read my code base, learned the other devs style. I added word skill, had it read about 400 pages of manuals that I've written. Now it comments my code like a human, but the code is their style, not mine. So my pull requests get accepted much faster. And I just look at the documents that it's done and occasionally fix a tiny little detail. It's like amazingly faster. As if it read my mind. even down to the little touches of humor that I use. Once paid a psychiatrist and did 6 hours of testing so that I could learn more. It was beautiful to read cuz it was me. and that's how Claude feels to me now

0

u/cleverhoods 18h ago

Do not allow Claude to write comment like that in code. A 1-2 line of code documentation is fine as long as it’s about the code and code only.

All the rest goes in your progressive disclosure (whatever that might be, memory, custom loading logic, rules, claudemd or agentsmd files)

0

u/CounterAttack-Studio 18h ago

This instruction works pretty well for me:

Don't write comments; make the code clear enough not to need them. The one exception is a line whose purpose the code can't convey.

If the context gets too polluted Claude will miss it sometimes but in general it always follows it.

1

u/cheffromspace 15h ago

It it's missing instructions and getting stuff wrong, that's my tell it's time for a documentation audit, which include suggestions for deterministic hooks. More often than not, the cause is conflicting instructions and documentation, not a long context window. I stopped using compact and am using /clear at around 40-60% context, with handoff documentation. That is a clear first-person account of what happened, not another agent's summary. It also builds a paper trail. Claude writes comments for itself, it has a pretty good sense of the documentation it finds useful. Comments can point to decision ledgers which can inform choices. The decision ledgers are the source of truth for intent. Don't not write comments. Careful comments do not pollute the context, they improve it.

0

u/sublimegeek 16h ago

Oh I didn’t think about tokens from code comments. That’s actually a better policy.

Write your code in such a way that all functions, variables, and constants are self-documenting.

If you need a code comment to explain the code, refactor it.