Hi,
Sorry to start with bad news, but don't ever call
main()
, in this case you just want the function to end naturally, control will return to
main()
by itself. This is how functions work, they either return a value or for
void
functions, they just end. So delete line 31.
I wanted to post my latest program in its entirety to get feedback on my comments but code must contribute to the post character limit. Instead, I want to ask what is "the norm" for program commenting in the real world? What types of things generally merit comments? What makes a "good" comment? |
First of all, if one has really good names for the variables and functions, then the code itself becomes self documenting without any comments. Reading the code should be like reading a story. Here's a comical version of what I mean :+)
To achieve this, avoid abbreviating names, and by having variable names that look too similar. I am not fond of
i
and
j
in the same context - it can cause problems. Also name variables for what they are: E.g. Row and Column, not
a
and
b
, or
i
and
j
.
One can have single char names when if it's a mathematical context, if the formula from wiki or elsewhere uses variables
a, b, c, d
, then it's ok for your code to use them too. For example, this is probably fairly obvious:
y = r * sin(theta);
, but some might even argue for :
YOrdinate = Radius * sin(theta);
Code is easier to follow when it's the same form as the documentation it comes from.
Put comments in for web addresses of the documentation you are using - the wiki page for example, or any other web page which might be useful. I have put comments for the actual formula with all it's Greek characters and exponents in, just before the function that does that calculation, so it's clear to me, and to others.
Comments about what a function does, and it's expected range of valid values, and it's expected output (pre-conditions & post-conditions) are very useful - one should almost always do that. There is a utility call Doxygen which helps generate documentation about your code: it uses doxygen comments to do so.
Put comments if you have done something tricky or unusual.
Don't put comments for something that is obvious in the code.
Here is some other really good reading from Bjarne Stroustrup and Herb Sutter:
Good Luck !!
Edit:
If you have lots of code, try posting it over several posts. If you really have a lot, put it on a hosting service like PasteBIn or GitHub or many others.