Successful Strategies for Commenting Your Code 500
LilG writes "Over at Particletree, Ryan Campbell writes about Successful Strategies for Commenting Your Code. His essay gives advice and examples on proper commenting, and details some different strategies."
Huh? (Score:5, Funny)
Re:Huh? (Score:3, Funny)
Was that some form of early DMCA or IP lockdown?
Re:Huh? (Score:3, Funny)
Re:Huh? (Score:2, Funny)
* slashdot comment comment
* 1.0.0 - torpor - init.
*/
#pragma KARMA_HACK
Obfucksucated code contest (Score:3, Funny)
Re:Huh? (Score:5, Funny)
*ducks*
My favorite code comment not written by me (Score:5, Funny)
Re:My favorite code comment not written by me (Score:5, Funny)
I was in a similar spot a month or two ago and found some other company's comments - unhelpful as always - in the form of typed out sound effects.
# This function goes vroooooooommm-pop!
I have no idea why the developers put such comments there other than to entertain themselves as they sifted through their horribly written Perl.
Re:My favorite code comment not written by me (Score:5, Interesting)
What makes a good Comment? (Score:5, Interesting)
a) What you're doing.
b) Why you're doing it.
c) How you're doing it.
I took three assembly programming classes in College. The last one was on the 68k, where we wrote an embedded OS.
Assembly code isn't all that intuitive, and writing comments is especially important. Our professor allocated 20% of our grade on each lab to comments. In addition to accurately describing what we were doing, he checked our grammar. One thing he always stressed is that too many engineers these days don't know how to write comments. Grammar is important in getting the message across unambiguously.
In general, if a person can read your comment and then figure out how to translate what you said into code, then your comment is pretty good. It should give an idea of what you're trying to do, why you're doing it, and how you're doing it.
One of my professor's grad students translated a program from MACRO32 into C++, and all without even knowing MACRO32. He looked through the comments to figure out what they were doing. They were so specific that he could easily translate blocks of code over to C++.
Comments are very important - and I should definitely comment MY code. I can't remember the number of times I've come back to code that I've written and thought "WTF am I doing here? WTF was I smoking when I wrote this?!"
Re:What makes a good Comment? (Score:3, Interesting)
I call B.S. ALL Engineers KNOW how to write comments. If they don't know how to comment they're code they aren't an Engineer. In fact I will lay odds the 60% of the "Software Engineers" out there aren't really Engineers. Just because you have a degree in Computer Science doesn't make you an Engineer. I know plenty of "Software Engineers" who don't know what an ABET accredited engineering program is.
All
Re:What makes a good Comment? (Score:5, Insightful)
As long as you're commenting the _real_ effect of the instruction, and not just mindlessly repeating what the mnemonic already tells the reader. I've seen lots of stupid assembly-language comments like "increments the A register". Gee, thanks - I couldn't have figured that out from the "INC A" mnemonic.
Re:What makes a good Comment? (Score:3, Insightful)
Re:What makes a good Comment? (Score:4, Informative)
Or worse yet, lying.
One pre-paleolithic piece of mainframe assembler I (and my colleagues) once worked with was an intricate sequence of shift operations designed to isolate a 5-bit segment in the middle of a 36-bit word in one of the accumulators. It was documented with a breathtakingly detailed block of comments, complete with little pictures of the contents of the register at each step in the process (composed of dashes and vertical bars and everything--ASCII documentation art).
The code wasn't working, but there was absolutely no way that it was the code's fault. It was just FM (F'ing Magic) that it was broke. We were convinced of this. That is, until one of my supervisors, a crusty old communications sergeant, actually studied the instruction string of the segement of code in question and noticed that in the last step, the actual answer was being shifted the wrong direction--right out of the register and into the bit bucket.
If only we could have compiled the comments...
This, then, is Johnson's Postulate on Documentation: "Read the source code, too."
Re:What makes a good Comment? (Score:3, Insightful)
Not specifically regarding assembler, but you've just reminded me of the thing I absolutely hate the most which is people taking a chunk of existing code, comments an' all, tweaking the code to do something similar but different, but not updating the comments. Any typically the people (they ain't programmers!) who do this also use the existing function name but suffix it with a "2" or something -- I mean, why bother with a meaningful name? Someone might be able to understand the code!
it was hard to write (Score:2)
but not really, commenting as you go has always worked best for me, going off an uncommented API is evil when it is complex, i loathe working on some projects because there is absolutly no documentation
Re:it was hard to write (Score:2)
Like this piece of code?
for(teamCount = 0; teamCount < defender.team.length; i++) {
It's not the comments that are making this code confusing - it's the fact that 'i' is not defined in the function, and thus must be a global. Having a global named 'i'? Now that's bad code!
Methinks that perhaps they meant to write:
for(teamCount = 0; teamCount < defender.team.length; teamCount++) {
Gotta say it (Score:2, Funny)
Example (Score:4, Funny)
No (Score:5, Funny)
This loop starts at 1, and to 5 it counts. It doesn't count to 6, nor it does count to 8. It does not count to 3 or 4, except in passage to 5. When it reaches number 5, the fifth number...
Mod parent way up :) (Score:2)
and give him some Underrated, not just karma-less Funny.
Re:No (Score:2)
Re:No (Score:4, Funny)
Re:Example (Score:2)
All in all, I found that the article's guidelines matched pretty closely to my own commenting style. I second the author's recommendations.
and don't (Score:2)
Don't appologize for your code, then promise to fix it later. I saw that done at one company, 3 years after the commenter have left.
x<6 (Score:2)
comments (Score:2)
code
code
code
#not sure what, but it looks important.
The ParticleTree... (Score:2)
var names (Score:3, Insightful)
Re:var names (Score:2)
Practice what you preach (Score:5, Funny)
Comments are more important than Code? (Score:5, Funny)
I once tried writing code that was completely made up of comments. It was easy to write and all, but didn't work very well.
Re:Comments are more important than Code? (Score:5, Funny)
Re:Comments are more important than Code? (Score:5, Interesting)
By front-loading the process with comments, I find it really helps not just the maintainability but it also keeps my programming tasks on track.
comments? (Score:2)
Comments? We don't need no stinkin' comments!
Why would I want to risk my future job security by letting other people know what I am doing?
Seriously, we all need to comment. But, how desperate are we for topics when we have a slashdot submission on "commenting strategies"? All you need to do is get all of the developers in a room and spend ten minutes talking about what should appear in comments. Then, enforce the rules with code reviews.
TODO (Score:2)
Re:TODO (Score:2)
Don't comment (Score:5, Funny)
http://www.cenqua.com/commentator/ [cenqua.com]
Successful strategy (Score:2)
* FIXME - Should handle slashdotting better.
*/
I was skeptical at first regarding the topic (Score:5, Insightful)
I think it was Fowler who once went as far as to state that when you find yourself needing to document code in order for it to be understood (vs. thoroughness, completness, or document generation) you probably need to refactor your code.
How many times have we come across code like this?
public void bigFunction1()...
public void bigFunction2()...
Re:I was skeptical at first regarding the topic (Score:3, Insightful)
Commenting (Score:5, Insightful)
And for crying out loud update the comments when you change code!
*grumbles about 10 year old code and 15 year old comments*
Tombola! (Score:2)
Tim
Re:Commenting (Score:4, Insightful)
really being stressed in schools (Score:3, Informative)
Re:really being stressed in schools (Score:2)
Re:really being stressed in schools (Score:2)
- It's hard, and it's hard to know that you got it right: Getting code right means the program works. Getting the comments right and there might be a payoff months in the future.
- Lots of programmers aren't good writers. Comments are written for people to read.
Re:really being stressed in schools (Score:2, Informative)
Summary: Though the next generation of coders may be more diligent about including comments (at least initially -- though I'm not sure I'm even convinced on this point), there is nothing that leads me to believe the content and ap
Re:really being stressed in schools (Score:2)
Re:really being stressed in schools (Score:2)
They definitely preach commenting for other reasons also. I helped my brother-in-law for Comp Sci 1, and his professor made them comment to the point where there were 10 lines of code and 20 lines of comments.
What's So Hard About Comments... (Score:3, Interesting)
Re:What's So Hard About Comments... (Score:2)
Real coders comments for C++ code (Score:2)
C++ means never having to .
Don't tab inline comments! (Score:2)
Dlugar
Re:Don't tab inline comments! (Score:2)
There's nothing worse than loading up a project someone else wrote and had their tabstop set at one space. I think they do that on purpose
Re:Don't tab inline comments! (Score:2)
Re:Don't tab inline comments! (Score:2)
If it gets really bad, there's always: Ctrl+Home, Ctrl+Space, Ctrl+End, Ctrl+Alt+\
A comment on comments (Score:5, Insightful)
The following is an example of useless documentation:
frobWoggleFfloofMoing
public void frobWoggleFfloofMoing(String, String, String)
...
You see, running Doxygen over your header files may produce some output in HTML format, but it doesn't produce what I like to call "documentation." For instance, documentation would explain what is a Woggle, and when should it be Frobbed?
Thank you, and have a nice life.
Re:A comment on comments (Score:2)
Re:A comment on comments (Score:2)
Couldn't agree more. There is nothing like the sinking feeling when you see a project uses Doxygen because it almost always means the "documentation" will be useless. Doxygen is useful in its place, but that place is only as *part* of a documentation process.
Aaarrghh!
Roger
Depends how you measure success (Score:2)
If success is defined as enhancing your job security, then boobytrap your code with misleading comments to make it impossible for anybody else to work on it.
Best strategy for comments, so far... (Score:2)
My commenting habits (Score:3, Interesting)
Why I comment code... (Score:5, Interesting)
Re:Why I comment code... (Score:2)
Two years down the line? I comment code so I remember it two DAYS down the line.
I've lost my train of thought.
Environment variables... (Score:2)
I would imagine that the type of work environment has a lot to do with the amount and quality of commenting as well. For example, I don't work in a commercial production environment; I do R and D in a government agency, which means fairly soft deadlines. I have a lot of respect for people that pump out apps that actually work, and am fairly forgiving of bugs in commer
Assembly code (Score:2)
Things to always remember when commenting (Score:4, Funny)
2. Use randomly chosen variable names, or objects that resemble your favorite Orcs and Trolls from LotR - after all, everyone knows that a Lothlorien object will have farseeing ability, so it's obvious.
3. When instantiating something for the first time, never explain it - real programmers read the original object source.
4. If you do something complex, write a short pithy comment like
5. If you do something easy but you were drinking too much hot cocoa, write a long verbose description, and also mention how good the hot cocoa was.
6. Always include song lyrics to what you're listening to while you wrote the code.
7. Object inheritence means never having to explain the code.
8. Repetition is the best way to reinforce obvious things - so repeat the obvious thing since it's the best way to reinforce it.
9. If you break up with your girl/boyfriend, write about it in the comments - people really want to know.
10. If you're updating or modifying code, write your opinion about the original code in the comments. Use nasty words if you can.
Re:Things to always remember when commenting (Score:3, Funny)
All Depends... (Score:4, Funny)
# The main function starts here
or...
# This is a loop and it will run while a certain
# condition exists.
or...
# Don't forget to remove this section after
# I'm gone - Dan - 04/25/1995
Think about the children! ;P
How & Whys (Score:4, Insightful)
Comment the why, not the how.
Hopefully the code should be clear enough to pick out the how, but *why* you are doing something is never going to be apparent from the code itself - at best someone might be able to infer it, but that becomes especially tricky when time passes or a new person signs on.
As Always, Just Out Of Reach (Score:2)
Today's Excuse: Well I would have commented my code just like they said to, but the site was Slashdotted so I couldn't learn how to do it right. By the time the page finally loaded it was time to deliver the project. But I really, really, REALLY promise I will comment my code next time. Yes, really!
Mirror (Score:2)
My simple strategies (Score:2)
Someone suggested this simple strategy to me a few years ago and I'm surprised at how well others pick up my code quickly. It really does make a big difference.
My take (Score:3, Interesting)
Anyway, this is the strategy I've come up with after having to go over my old code.
At Least Be Honest (Score:2)
'not quite sure what this bit does'
It's been like that for years, but whatever it does, it seems to work.
zerg (Score:5, Insightful)
Comments should reproduce code (Score:2)
This is what I do; I've always thought comments should allow one to reproduce the functionality of the code just by reading the comments. Also makes it much easier to understand what you've done when you or someone gets back to it later.
What about unit tests? (Score:2)
But he doesn't mention unit tests at all, which I think are one of the best places to define what code is for. Unlike regular comments, unit tests can be verified
Next on his list of things to write about (Score:3, Funny)
Hasn't this been covered already? (Score:2)
Or did we really wait 30-40 years to bother to write up something on how to comment?
Doxygen all the way (Score:2)
Literate Programming (Score:3, Interesting)
Two words: Literate Programming [wikipedia.org]
Use intuitive functions... (Score:2)
To ensure readability, make sure to translate all iterator use and loop constructs to recursive function calls and definitions.
Besides, if the next programmer can't deal with it, he's unworthy
disagreed on a number of points (Score:2)
I'll throw in some disagreement about a couple of things. Code commenting has one purpose: to communicate to a developer at some time in the future how some code works, so that he can adapt/fix it. There is also interface documentation (ie javadoc) but I consider that an essentially solved and separate issue.
Hungarian notation is bad because it discourages descriptive naming. By forcing you to add extra characters to a name indicating type,
Just Don't (Score:2)
Write intention-revealing code. Use meaningful identifiers. Write with the maintenance programmer in mind. Code is write-once, read-many. In the days of low-level programming languages and maximum identifier length, comments were at least necessary to explain WTF a PLGRNX1 means. With modern development tools, you don't even have to type out the whole identifier after the first time, you can use code completion.
Too many comments makes me wonder why the programmer couldn't write clearer code.
Comments: Why, Sometimes What, Never How (Score:3, Interesting)
1. Use comments liberally to explain why. Why would I want to call this method? Why did you choose this particular algorithm? Why are you ignoring this exception? Source code has very little support for explaining why without comments - and why is often both the most important point and the most easily forgotten.
2. Use comments occasionally to explain what. Your method names, parameter names, and logic should strive to be clear enough that explaining what is happening is unnecessary. But that is in a perfect world. If you can't make it clear in a reasonable amount of time, add a comment explaining what is happening.
3. Never use comments to explain how. The real how diverges from the comment too quickly. If someone needs to see how, they should look at the source code, which is the only reliable resource for explaining how.
There are exceptions to all of these I'm sure, and this doesn't cover everything (for example, the above says nothing parameter definition comments or copyright comments), but they're decent rules of thumb for most cases.
Flip the Script (Score:3, Interesting)
This discipline is tied to my code layout style. I put braces ('{' and '}') each on their own line, in languages which use them to delimit blocks of code (most of the languages I use). After the opening brace, I put a single space, then a comment, like:
# Use list if it has items.
if($numEntries = $#list)
{ # Get personal info from people in the list.
Then I indent the block with 4 spaces, starting with a comment, like:
# Use local storage.
my(@names, @addresses, @jobTitles, $name, $address, $jobTitle);
Loops get described as a single operation, but their block describes each iteration's operation:
# Get columns into separate lists.
foreach $row (@list)
{ # Add fields to their columns.
# Get fields from row.
($name, $address, $jobTitle) =
($row =~
# Store fields in their columns.
push(@names, $name);
push(@addresses, $address);
push(@jobTitles, $jobTitle);
} # each row
} # Used list of people.
The comments, layout and variable names make it easy to understand what's happening. So easy that it's useful to read them first, then write the code
Re:Flip the Script (Score:3, Interesting)
It wasn't terrible, but the result was a lot of comments that were echos of the code, and an unknowable amount of code that should have been broken out into discrete functions, but was instead kept nearby its comment because the commen
Re:human readable (Score:2)
Then good comments are... what? Undesirable?
Re:human readable (Score:2)
A non-programmer should be able to determine the what, but the what should come as much from the naming choices as from the comments.
If your comments are explaining how the language works to the point that a non-programmer can understand it. You've gone too far with your comments. Long before this point, the actual code that programmers will need to maintain will have become obscured by the comments and you will have made the code harder to maintain.
In gene
Re:How much do I comment? (Score:2)
I /* singular /* adjective /* noun /* noun /* noun /* noun /* noun /* noun ....
Re:indeed (Score:3, Interesting)
Comments should be common sense and used where the developer thinks it is appropriate and where they will aid future developers/maintainers understand a particular block of code. Nothing more, nothing less. The article is just typical PHB management bullshit and I feel dumber after reading it.
The scenario that is the ultimate end product to "comment standardisation" is what happened at a company that a friend of mine works for. They had a developer sit there for 3 days, go t
Re:indeed (Score:2)
Re:indeed (Score:3, Informative)
The first is that common sense isn't. There are lots of coders who seem not to have any.
Second, what seems clear and obvious when you're writing the code is anything but to someone else who's approaching the code fresh. That "someone else" may very well be you six months or a year down the road. Every function over one line (and most of those) should have a comment indicating what it does. Any variable that isn't a throwaway loop counter or similar should either be so cl
Re:Don't do it! (Score:2)
Me, I hope to be able to pass this project on to some junior dweeb in a year or so, and move on to something New and Exciting.
Re:Don't do it! (Score:3, Insightful)
Don't EVER comment your code if you work for a company. That's a sure fire way to lose your job! If you don't comment your code then you are the only person who can understand it, making you indispensible.
Great idea! The myth of indispensibility is a great reason. May your next job be as a maintenance programmer in a Perl shop where the previous "indispensible" developer had his ass fired for threatening the boss and you get 50,000 lines of Perl dumped in your lap.
Re:Don't do it! (Score:4, Informative)
1. You are likely to be passed by on promotions because self inflicted developer dependence for this application.
2. You will have to figure it out later, after you've forgotten what all that magic gobly-gook does.
magic gobly-gook: effective, efficient, and incredibly dense code produced wilst 'in the zone'
Re:All the commenting you need (Score:2)
Should read:
Re:Klingons and code documentation (Score:3, Funny)