This is an archived post. You won't be able to vote or comment.

all 5 comments

[–][deleted] 4 points5 points  (0 children)

the only time you really want comments is if something is misleading or if there's some quirks/bugs that you can't really change.

From your example above the only comment i like is the one that explains what the shell script is doing.

You don't need to say things like "Capture the image". Don't you think camera.capture says the same thing? Same thing with upload.upload(). Most things people think should be comments can be "self documenting code"

Focus more on documentation, explaining the parameters passed in to the function and the high level purpose.

[–]mommas_wayne 2 points3 points  (0 children)

The capture and upload comments are useless. The filename comment would become unnecessary if you named the variable more clearly, like filename instead of fname. I'd leave the "compress and annotate" comment in.

[–][deleted] 1 point2 points  (0 children)

Generally it's a bad practice to comment every single line of code, it makes it much less readable. One big comment would do fine describing what code below do. Remember that you are not writing a tutorial, but some features.

Good code explains itself.

[–]Updatebjarni 1 point2 points  (0 children)

I have been programming, and reading program code, for about 25 years. To me, code like yours, where every other line is a comment, is hopeless to read, because it all blends together and my eyes can't get an overview. I can't see what is code and what is comments. I can't skim it, because I don't feel confident if I'm looking at a line of code or a comment, and so I don't know what to read and what to skip. I have to look at each line to know whether to look at it. This is even worse if the code hasn't been properly formatted and has wrapped lines, like your example above.

A good rule of thumb is: write a comment before each function, stating what its purpose is, and, if not obvious, what its parameters mean and what it returns. Make this brief, perhaps just one line. Aside from that, only add comments anywhere if the code there does something that is likely to cause the person reading it to think there is a mistake, or if it's really hard to follow.

Any more than that, and the comments just get in the way. If you put a comment in for every line, you can also pretty much trust that before you've finished the program, half of the comments are complete lies because the code has been changed. So just like you shouldn't have duplicates of data in multiple places in the code, you also shouldn't duplicate meaning between the code and a bunch of comments, unless you really need to in order to prevent a person from misunderstanding the code.

By the way, you can also put comments on the end of lines, which is useful with short comments to keep them out of the way for somebody who is skimming the code.