Minimal Advice to Undergrads on Programming
cscs.umich.edu
cscs.umich.edu
My general policy is to follow the "Use meaningful names" policy and use that as the commenting. The meaningful names of the function give you an idea of what they do and what side effects they have. The meaningful names of the parameters tell you what they do.
Maybe it's not a perfect system, but I think it's a happy medium between eating up a lot of time commenting and having totally unreadable code
Comments themselves have, especially when you have lots of them, a really high danger of not getting updating and therefor becoming a liability.
When you write code you are supposed to write (1) the tests, (2) the documentation and (3) the actual code. Only one of those can make what you want to happen happen, and one of those is annoying if it doesn't get fixed but one of those wont do anything if completely wrong. At least not right away.
If there are assumed states or input ranges, asserts are better than comments. Comments drift out of correctness, asserts don't.
I try to design data and configuration files with comments and then comment them with field descriptions and examples.
As an undergrad, it's probably better to err on the side of too many comments. Then trim them down later as you get better at it.
Good comments also probably helps your prof grade your work.
These days, though, TDD is winning a lot of converts as a more effective means of specifying the expected behavior or code. Whether you consider that a good thing probably depends on your relative opinion of executable vs. human-readable specifications.
And agreed, code with many short, _well-named_ functions is generally more readable than code with a few cumbersome, but heavily documented ones. Also, the places where I'm inclined to put a line comment are usually natural boundaries for extracting code into its own function.