The following stuff is highly a matter of taste, not something that I consider 'correct' in any way
quote:Original post by CWizard
Well... I do It's a habit of mine.
Hmm, overcommenting is actually kind of bad. With such simple code, it's actually faster to read the code to understand what it does, than to read all those comments. And editing the code will be easier with less comments, since one doesn't have to change the comments after every small change in the code.
However, I commented too little (that is, none
). I should've commented what the first loop does and what the last loop does, since those are quite big structural elements. But in real life, I would've extracted those two parts in to their own methods and given them descriptive names like
readUserInputTo(data, total);
and
showDataInTableForm(data);
Then I probably would've just added a small comment before the readUserInputTo() declaration. Like
//reads positive float numbers until "end" is typed
quote:I always declare all variables I'm using in the function at the top with an explaination of it's usage in the function, so I easily can see what a variable is used to by looking at the top.
That's not a good way to do it IMO. Giving variables too much scope will more likely confuse the reader about the intent of the variable. (S)he may have to check through a lot of code to see if that variable is still used somewhere else, when it's not. If a variable has little scope, you can immediately say what it does, and that it doesn't do anything else. E.g. I declared "string buffer" inside that while loop, since it's only used there. Sometimes (but rarely) I move declarations outside loops though (right before them), since I think it's faster.
[edited by - civguy on August 15, 2002 4:25:08 PM]