Should I be adding comments to my beginner C++ programs?

0
5
Asked By MellowHarbor42 On

I'm taking an introductory C++ class, and the assignments are still pretty simple. If I understand what every line does and can write a program that solves the task, is that enough, or should I still be adding comments? I'm especially wondering whether comments are expected even when the code seems self-explanatory.

4 Answers

Answered By CedarLark7 On

Check your syllabus or assignment instructions first, because your instructor may specifically require comments. In a class, comments can also show your reasoning and demonstrate that you understand the solution, so a short explanation of the approach is usually worthwhile. You don’t need to comment every single line, though.

Answered By OrbitMango5 On

A good beginner habit is to write a brief plan before implementing a solution, such as the major steps or stages the program will follow. Once the code is written and readable, remove temporary comments that merely repeat the code. For functions intended for other people to use, document their purpose, inputs, outputs, and any important behavior.

Answered By NovaBiscuit19 On

Try to make the code explain what it does through clear variable, function, and class names. Comments are most useful for explaining why something is done, especially when the reasoning, constraints, edge cases, or side effects aren’t obvious from the code itself. A comment like “increment counter” adds little if the code already makes that clear.

QuietPine88 -

This also makes maintenance easier. What seems obvious while you’re writing it may be confusing when you return to the project months later.

Answered By RiverCobalt31 On

Comments are mainly for your future self and anyone else who has to maintain the program. You don’t need exhaustive notes for a tiny exercise, but do document complicated logic, unusual decisions, workarounds, and anything that would be difficult to infer by reading the code. Keep comments accurate, since outdated comments can be more confusing than no comments at all.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.