Commenting is entirely creator dependent, so we can't tell you exactly how to comment it.
That being said, here are some practices I have learned to help others understand.
1) Have a header section with a brief outline of what the code does, when it was made, and possibly updates done to it. This can be especially useful if you have multiple versions of the same code and weren't sure what was changed.
2) Use section breaks to seperate different portions of your code. Often I like to keep inputs, processing, and results in separate sections.
3) Outline the purpose of things. We all know what are for loop does, but WHY it is being used is not always clear. This is really helpful for others to see when they are trying to understand your code structure.
It's impossible for someone else to say. Depends entirely who needs to understand it. Some code doesn't need commenting at all - if it does it's badly written. Other code needs comments however well written it is. I would recommend using variables with meaningful names instead of hard-coded 'magic numbers' like 9.81 and 0.8444, but I suppose comments on those would work well enough. I assume the 9.81 represents gravity so personally I would define
gravity = 9.81;
somewhere at the top and use that. Normally I define such things on the line before they are first used, but obviously not if that is in the middle of a loop.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
3 Comments
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/487808-how-do-i-properly-nicely-comment-this-code#comment_760738
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/487808-how-do-i-properly-nicely-comment-this-code#comment_760738
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/487808-how-do-i-properly-nicely-comment-this-code#comment_760739
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/487808-how-do-i-properly-nicely-comment-this-code#comment_760739
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/487808-how-do-i-properly-nicely-comment-this-code#comment_778138
Direct link to this comment
https://ch.mathworks.com/matlabcentral/answers/487808-how-do-i-properly-nicely-comment-this-code#comment_778138
Sign in to comment.