How Much Should I Comment My Beginner C++ Code?
Asked by
MellowPine42
I'm taking an introductory C++ class, and the assignments are still pretty simple. If I understand what each line does and can write working code that solves the task, do I still need to add comments? Should I be documenting every line, or is there a better approach to commenting beginner projects?
Answers
A good rule is to comment the parts that are non-obvious or likely to be misunderstood: complex logic, unusual constraints, edge cases, temporary workarounds, and the purpose of public functions or classes. If a section is hard to explain, first consider breaking it into smaller functions with clearer names. Donโt use comments as a substitute for readable design.
Reply to this answer
There isnโt one universal workplace rule: some projects expect extensive documentation, while others prefer minimal inline comments. In real codebases, follow the projectโs existing conventions. For your class, prioritize whatever the instructor asks for, and otherwise include concise comments that clarify your approach rather than filling the file with line-by-line explanations.
Reply to this answer
Aim for self-explanatory code first: use meaningful variable and function names, keep functions focused, and format everything consistently. Comments should usually explain why something is being done, not repeat what the code visibly does. A comment like โincrement iโ adds little value, while โskip cached entries because they were already processedโ explains useful reasoning.
Reply to this answer
Check your syllabus or assignment instructions first, since your instructor may require comments to show your reasoning. For schoolwork, brief comments can demonstrate that you understand the approach, especially when the solution is not obvious. You donโt need to narrate every line, thoughโdescribe the overall steps and any important decisions.
Reply to this answer
While youโre learning, writing a short plan in comments before implementing the code can be helpful. Outline the major stages, then replace those notes with working code as you go. This helps you practice translating an algorithm into steps without creating a comment for every single statement.
Reply to this answer
Add your answer
Answers are reviewed before publication.
CedarMoon56: Comments are also useful for your future self. Code that seems obvious today may be difficult to understand after you have spent months away from the project.