Three Comment Elements and Code Protection
AI-generated comments mostly just restate what the code does. Come back three months later, and you can't remember why it was implemented that way. This lesson provides a structure for comments and establishes rules for "deleting code". The page includes two interactive demos.
Code can only express "what was done." Why it exists, why it was implemented this way, what to be aware of when calling it — this information can only persist across time if written in comments. "Write good comments" is a phrase AI cannot execute on; you need to provide a fixed structure and examples.
Context
What business problem this function solves and in what scenario it is called. Without context, the reader can only see the implementation, not why it exists.
Design Intent
Why it was implemented this way, the reasons for choosing this approach, and which alternatives were abandoned. This can't be found in git log; comments are the only carrier.
Key Constraints
What callers need to know: side effects, dependencies, edge conditions, and other non-obvious caveats. Without this, the next caller will step on a landmine.
Click to switch between two comment styles for the same merge_chat_history function, and compare the amount of information each preserves.
Three real-world scenarios — judge what AI should do. Click an option for instant feedback with the relevant rule reference.
Correct so far: 0 / 3
Comment Protection
During refactoring, it is prohibited to delete context and design intent comments on the grounds of "the comment is too long," "the code is self-explanatory," or "cleaning up in passing." If an implementation change makes a comment inaccurate, the comment must be updated in sync. The only test: could someone taking over the code understand why it was done this way without this comment?
Code Deletion Declaration
Before deleting any existing functional code, you must explicitly inform the user with a reason. Silent deletion on grounds of "cleaning up in passing" or "looks unused" is prohibited. When you believe a code block should be removed, first annotate it with // TODO: Suggested removal - Reason: xxx and delete only after receiving approval.
No empty catch blocks. All try/catch blocks and error branches must have substantive handling: logging + a user-visible error message, or a reasonable fallback. A bare console.log(e), pass, or // ignore all constitute silent error swallowing and are never permitted.
The mission of comments is to preserve decision-making information that code cannot express. The three-element structure gives AI a format it can follow; the protection rules prevent it from deleting them. Together, they let knowledge persist across time.