Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comments in source code [closed]

How to keep the source code well documented/commented? Is there a tool to generate a skeleton for comments on the Unix platform for C++?

In general, how many lines of comments is recommended for a file with around 100 lines of code?

like image 911
Shree Avatar asked Nov 27 '22 20:11

Shree


2 Answers

Generally, it's best to let the code itself explain what it does, whereas the comments are there to describe why it's like that. There is no number to stick to. If your 100 lines speak for themselves, don't comment at all or just provide a summary at the beginning. If there is some knowledge involved that's beyond what the code does, explain it in a comment.

If you're code is too complicated to explain itself, then that may be a reason to refactor.

This way, when you change the implementation you don't need to change the comments as well, as your comments do not duplicate the code. Since the reasons for the design seldom change it's safe to document them in comments for clarity.

like image 126
Michael Klement Avatar answered Dec 10 '22 19:12

Michael Klement


Personally I think skeleton comments are a horrible, horrible idea. I understand that sometimes it's nice to save couple of keystrokes and perhaps get argument signatures in comment... but resulting n+1 empty useless comments (when editor has added boilerplates and coder has left them as is) are just more irritating.

I do think comments are needed, at any rate -- if only code one writes is too trivial to ned explanation, chances are code in question is useless (i.e. could have been automated and needn't be hand-written). I tend to comment my code reasonably well because I have learnt that usually I need it myself first. That others can use them is just an added bonus.

like image 35
StaxMan Avatar answered Dec 10 '22 20:12

StaxMan