The Importance of Commenting and Documenting Code?
mrtrumbe asks: "The company I work for is in the process of creating a development standard to be applied to all projects. The topics being considered range from dictating the formatting of the code (an issue on which there is widespread agreement), to creating a standard for commenting and documenting the code (a far more contentious issue). On the issue of commenting and documenting, there are two extreme views being considered with most employees' opinions falling somewhere between them." To comment, or not to comment. And if you do choose to comment, what's the best way to standardize it, company-wide?
"The first view is that commenting and documentation will protect the firm from bad programmers or a programmer abruptly leaving, make the code far easier to understand to someone unfamiliar with the codebase, and are necessary for all public, private and test code. The opposing view is that there are more effective ways to mitigate the risk of bad and disappearing programmers (like mandated shared ownership of code and sufficient oversight), that comments are not necessary for clarity and can be dangerous if not kept up to date (which is considered likely), and that documentation is necessary only for public code. Where does Slashdot stand on this issue? Please share any success stories and recommendations for a company-wide standard on commenting and documentation of code.
If your code is not commented it's not complete. My advice is to fire every developer that doesn't think that comments are necessary.
I keep seeing all these arguments either against commenting or against verbose languages because, supposedly, they slow development.
Now, Maybe I've just been programming too long and have gotten too good at it, but typing is never ever a slow-point in coding; heck, even learning a new language doesn't slow you down too much!
The slow part is designing your code correctly so that it's fully factored and as bug-free as you can manage--this takes thought and a bit of time, but no where near as much time as it would take to do the same release with cut & paste (I've seen it many times).
So I'm trying to figure this out, why are people making these arguments? Is it that for unexperienced people it truly is harder to put comments in with your code? Maybe they don't know how they did their magic and don't want others to figure them out? Maybe they never took a typing class and it truly takes more time to code than think? I'm really at a loss here.
Oh, and as for the authors question, you have a FANTASTIC opportunity to improve your company tenfold. Take notes of those arguing against commenting. As soon as you've collected all the votes, throw them away and FIRE anyone who was against documentation--they should not be working in any company, at least not as a programmer! If you hired people who understood programming and the development cycle, that question would have never come up.
You're misunderstanding the real purpose of comments.
If your comment says "Increment i" and the code says "--i" then yes, things are fucked. But the purpose of comments is not to describe *what* the code does but *why* it does it (and occasionally *how* as well if it's not clear, for example if there's some particularly gnarly maths or pointer weirdness involved).
Anyone writing comments saying *what* their code does what it does needs their code reworked at review - and if they're not on their first job then they need firing.
Anyone *not* writing comments saying *why* their code does what it does needs their code reworked at review - and if they're not on their first job then they need firing.
Grab.
A friend of mine told me a story about someone at a large engineering company here in the Northeast (details are being left vague to protect the guilty and/or insane). Here's what this amazing, completely mad person supposedly actually did:
In a huge software project, he named every single variable after a notable warship in United States History. If the variable was really important, it would be named after the lead ship in a battle group, like for instance an aircraft carrier or (WWI era) a dreadnaught. If the variable was just a little local variable, it would be named after something tiny, like a P.T. boat. Variables that participated in the same battles were used in the same modules. Variables' relationships mirrored their relationships in real life, so for instance, one variable would be named after a destroyer and a helper variable would be named after a tender.
Think about how utterly brilliant and devious this is!
The ONLY people who would have any chance at all of understanding the program would be anal-retentive naval history buffs! And the scope of it was supposedly amazing. If my friend was to be believed, this was an old-fashioned, NON-OO, structured-programming project with hundreds or maybe thousands of variables, all spaghetti code, everything named after fucking BOATS!
It's priceless.
"Little does he know, but there is no 'I' in 'Idiot'!"
That works in some companies. But be careful, it can bite you in the ass.
//Set i equal to 5 //get the element in the fifth slot of the array //array is zero based, so be sure and subtract 1 //from i beforehand //now that we have iBS we need to......
I recently took a promotion on another team within my company (a large Aeronautics firm). Because I was saddled with a high customer exposure product and given no support I am the guy that >"everyone needs to...ask for a fix when thinks [sic] break." I did an average job of commenting, I believe in using meaningful variable names to help self document the code. Lots of people I work with over comment ie:
i = 5;
int iBS = somebullshitarray[i-1];
I've also written and kept up to date several high level overview documents that help new developers get acquainted with the code quickly.
Unfortunately, I was handed my transition plan for moving to my new team yesterday. The >"employment insurance" you speak of came in the form of a transition that is slated to last through March of 2007. So sure, I have a job for another year minimum, but it's the one I've been trying to escape for the last 2 years. Not to mention that my new salary won't kick in until after the 50/50 transition point in June.
My point: Comments can be good if they are meaningful. If you are trying to protect yourself and your product to ensure it is maintainable, the best way is to ensure that more than one developer is familiar with each product. Always have a backup plan for one of your developers getting hit by a bus, or leaving for another team.
This sig has been removed pending an investigation.
"The ONLY people who would have any chance at all of understanding the program would be anal-retentive naval history buffs! And the scope of it was supposedly amazing. If my friend was to be believed, this was an old-fashioned, NON-OO, structured-programming project with hundreds or maybe thousands of variables, all spaghetti code, everything named after fucking BOATS!"
Intentional Programming would have dealt with that problem.