Myths About Code Comments
theodp writes "Jason Baker gives his take on the biggest misconceptions about code comments: 1) Comments are free ('When you update the code that the comment references, you usually have to update the comment as well'). 2) Comments make code more readable ('by far the most pervasive myth that I've encountered'). 3) You should comment every function, method, class, and module ('documenting something that needs no documentation is universally a bad idea'). 4) Code must always be 'self documenting' ('would you rather use a one-liner that requires a 3-line comment, or a 10-liner that requires no comments?')."
Well, everyone's welcome to their opinion, but it's pretty well proven after decades of software engineering that code should be commented. The price of maintaining comment-free code is well known.
There is a school of thought among programmers who consider themselves hotshots that if you are not a hotshot you have no business touching their code. The problem with this attitude is that it has little to do with the real world, where people change jobs and programmers inherit someone else's code. If you want to write perfect, comment free code in your perfect little world, go right ahead, but don't expect to make a living at it most of the time.
It's surprising to me that someone has submitted this as a "news" item. News flash! Everything you know is wrong. Sorry I don't buy that. If you don't comment your code, I won't pay you for it. I'll inform the management that you neglected an important step and don't deserve a good reference. I won't be able to give you the benefit of the doubt when your code doesn't make perfect sense. I'll trash talk your code in front of your colleagues. Look at all the mistakes in this guy's work; I'll sure never recommend him if his resume crosses my desk. We may need to just rewrite this stuff because it's not maintainable as written.
What's really annoying is when they put comments that don't elucidate the code or their intent; they're just snide little messages from one know-it-all to another. They're too embarrassed to actually explain the code because that implies a level of insecurity they would rather not admit to. So instead they say things like: /* yeah, I don't like this either */
or
# hack, to be fixed later
Wooooo, really helpful comments there. I've seen this sort of thing countless times in my career and most others I know have as well.
it's = "it is"; its = possessive. E.g., it's flapping its wings.
No Comment
Clean code tells you how, good comments tell you why.
Fixing the "how" becomes significantly easier when you know "why" the code was there in the first place.
That green slime had it coming.
Remember what is crystal clear to you may not be to the guy coming in to clean your mess up in a few years. ( or even yourself as you have learned more and advanced your skills, and have to go back, often with a 'wtf was i doing'.. )
Having to decode your thought processes places extra a burden on the 'next generation'
---- Booth was a patriot ----
Having worked as a programmer for many years, all I can say is poorly commented and undocumented code is unprofessional and I would rather
rewrite it than try to decipher it. I've heard all of these excuses before and all I can say to the people that make them is: Your code is not as
good as you think it is.
The difference between Canada and the USA is that in Canada healthcare is a right and gun ownership is a privilege.
I do think good commenting has to be a key part of software engineering (when it finally becomes a real engineering discipline), and I know software engineering is a hot topic these days, so perhaps there's a paper on this that I just haven't seen yet. If not, someone really ought to do a serious study. And then start teaching people something that's actually known to work.
--Greg
I disagree with this general statement:
First, I think any time you consider writing some clever one-liner in real production code you should rethink long and hard. Is the hypothetical 6 fewer lines of code really worth it? Unless you're doing it for absolutely required performance reasons, the 6 lines of code you save won't buy you anything but the wrath of whoever has to come along in 3 years and refactor your code.
Of course you can be too verbose, but it's a much rarer problem than the alternative. Even overly verbose code is usually easier to understand, easier to maintain, and easier to refactor than that clever little one-liner. And if you're that worried about a verbose section of code, extract a method, give it a good name, and be done. Now you have both a one-liner method call that is easy to read in context and a more verbose implementation that is easier to understand and maintain in the future.
"What do you despise? By this are you truly known." --Princess Irulan, Manual of Muad'Dib
/)
In other news, people have personal preferences, just as they do in the literary world. Some people like Ayn Rand, others hate her. Some like Stephen King, others hate him. Not so much their stories, but their writing styles.
Snowcrash is an awesome story; one many can appreciate, but most people couldn't get through his writing style to finish the book.
I think that if more people wrote code (or, for that matter, books) like George Orwell wrote his books, code (and books) would be generally more readable: shorter sentences (lines of code) which convey a single meaning (function), strung together into short paragraphs (code blocks), chained together into a larger cohesive whole.
~/ssh slashdot.org ssh: connect to host slashdot.org port 22: too many beers
I had excessive commenting drilled into me in college. It's one of the things I've had to unlearn.
Code that has concise, sensible comments is indicative of a good design. However, I don't care how 1337 you are, no professional developer worth a damn programs anything bigger than a 3-line batch without at least some sort of design document. Comments that relate back to the design document are one thing, but it is really hard to use comments to point out the design of a program which had no design other than "I am ok to just wing this because i am 1337."
Christ, I know everyone has their own personal style and everything, but this is just pernicious. In any case, the author gives the game away: when he thinks code is overcommented, he can ask Emacs to hide the comments. So far as I know there's no automatic system that will generate the comments that the author failed to put in because the code was "self-documenting". This is particularly important when you're working with anything other than standard libraries --- you might know what "libfzp_inc_param_count_fast", but your reader probably won't.
Right now I'm working on a crypto library that incorporates a lot of very specific elliptic curve operations. My technique is to comment the hell out of every damned interesting piece of code on the assumption that a picky reader can turn off the damned comments if they get in his way. In fact, there are various places where I've actually scaffolded all of the comments before writing a line of code. Doing otherwise would have been an enormous headache and made bugs a whole lot more likely. And this way even a non-expert should be able to understand the entire program flow.
Unfortunately, one of the previous pieces of software in this area followed the poster's "self documenting code" style (very nice, clean, well written code with no comments), and even I find it difficult to piece together what's going on in places --- not because all of the code is crypto-specific, but because the author has thrown so much effort into writing "clean, pretty" code that it's actually hard to know where the crucial pieces are. I can't quite explain why I find this so irritating, but perhaps some of you will know what I mean.
Comments are useful if kept up to date with the code. Otherwise they turn into bald faced lies or wishful thinking.
Code that follows a logical structure, proper use of white-space, well-name variables and methods, etc is far easier to read and debug than code that has a lot of comments. Comments are non-standard and difficult to search for when looking for a problem.
Comments are best reserved for things which are non-trivial to figure out by reading and following the code.
Work Safe Porn
It really depends on what language you write your code in. Object-oriented languages in general require less documentation since good design and properly named methods and properties do document things relatively well. Class-level documentation is much more important than documenting the details of the methods themselves. Java and .NET require much less documentation to maintain. I've looked over Java code I wrote years ago, and it still makes perfect sense, even though I've only really documented the API and not the details. Perl and C code, on the other hand, can be unmaintainable even with a number of comments, because the old functional design is not easily maintainable. Consider rewriting these in a more modern language.
Call me a hotshot if you want, but I shouldn't have to assume that the person maintaining my code is a moron, nor that my non-technical manager needs to understand every single statement I wrote. I play nice with QA, and happily write test cases for my code. That's as far as I'm willing to go to ensure that my code quality is good, and that I can leave without the possibility of my code degenerating into the spaghetti-like mess I've had to clean up on a number of occasions. Sure, I don't get hired to write code anymore, but I don't care. Software Engineering and overdocumenting every single tiny intranet project is wasteful. Cleaning up after morons who believe that their engineering background can make up for their lack of talent and experience is aggravating. Answering to them is intolerable. There's other things I can do with my technical background that don't involve butting heads with people who dogmatically apply software engineering methodology to everything they do. It's their loss, not mine.
"Please describe the scientific nature of the 'whammy'" - Agent Scully
I suspect comments in code are like project notes.
Even when I come back to my own projects a year or so later, it is obvious what I was doing with x widget attached just so the y widget.
It is rarely obvious WHY I came around to that solution.
Project notes, and I suspect good comments in code, address the though processes behind doing something this way, rather than explaining in detail WHAT the code does.
10 REM FOR 16 BIT NMS MACHINES
20 FOR X=1 TO 10
30 PRINT "HELLO"
40 NEXT X
(told you I wasn't a coder)
But line 10, the comment may be very useful, aha, that's why this works on X machine but not X machine, etc.
http://slashdot.org/~GuyFawkes/journal
If your comments are that detailed, you're doing it wrong.
No offence intended to him, but he's just re-interating the sort of commentary on commenting that's in Code Complete, The Practice of Programming, Linux kernel CodingStyle etc. He's not offering any original insights, or telling war stories that people can learn from. Any decent programmer would already know these myths, and if they didn't, they really should be reading books such as the ones I've listed, not a blog entry with very little original content.
IOW, what makes special enough to be Slashdot front page news?
The Internet's nature is peer to peer - 20050301_cs_profs.pdf
Depends on the efficiency and maintainability. A one-liner that's "perfect" is probably worth the extra comments. Ten lines of self-evident code may not be better. Personally, I can't stand code that starts "boolean done = false; while (!done) {...}" when the condition can be determined inline using an operational variable.
It must have been something you assimilated. . . .
Easy. 10 lines, no comments. After writing a couple of million lines of code, the more code I write, the more I unwind it. Somewhere along the line, adolescent programmers got the idea that jamming all your logic into as few unreadable lines as possible is the fastest way to manliness. Way, way wrong.
Modern compilers and interpreters do a pretty good job nowadays. Source code bytes are near free. If you have to skull out dense code 6 months after you've written it, you're doing something wrong.
(OK, Lisp and APL are special cases, but really, when's the last time you wrote Lisp or APL, other than for fun?)
Every line should be commented, like: // Declare function called doit with one int param that returns an int // See above comment // The function's open brace. I like to put braces on their own line. You should too!! BTW, this is C code, so braces are totally the way to go. // Check if i is 0. You know, in C, "==" is the way to compare values, unlike in VB where you use a single "=". Just thought u should know. // Return 0. That is, all the bits of the return value are 0. We could also return i, because i is 0, too. That is, all the bits of i are 0. On a 32 bit system, there would be, like, 32 0's. // Begin an if block using a brace (this is C syntax!!!) // Declare an int variable named j that is one less than i // Return the sum of i and the value of calling doit with j // Finish the if block with a C close brace. By the way, we could have written the above code as return i + doit(i - 1) without using the braces. // The function's close brace.
int doit(int i)
{
if(i == 0)
return 0;
else
{
int j = i - 1;
return i + doit(j);
}
}
There! Now that is both way readable and informative. Anything less would just not pass my code review.
If you don't header-comment your class or public method,
I have to conclude that you cannot articulate what it is or what it
means or is for. So probably the code is incoherent/inconsistent too.
So that's going to make me write my own instead of using your code.
Or maybe you can articulate what it is about but are borderline
autistic and don't see the need to communicate meaning to others.
To me this just means you are so inexperienced in programming that
you a) don't understand the costs of maintenance, and
b) don't even realize that you yourself aren't going to be able to figure
out your code in six months.
Either way, I'm not using that code if I can help it.
Where are we going and why are we in a handbasket?
myths about excessive code comments. Provide comments in places where the comment might be useful. If you have some obscure function, the purpose of which might be confusing if you or someone else looks at it a year later, then you need a comment. The more obscure or terse, the more detailed the comment should be. On the other hand, if the function's purpose is obvious just by glancing at it, or the function name is sufficient to describe the entire purpose of the function, there's probably not a need for any comments at all.
Within a function, if you only have nested loops one deep, there's probably no need to comment each closing bracket. But if you have some horrendous 12 dimensional loop with lots of if statements, it might save a few headaches to know exactly which bracket lines up with which routine.
It's just a matter of commenting what is necessary, and avoiding it when it's not.
-Restil
Play with my webcams and lights here
..very much for mentioning this. Screenshots so far look very promising.
---> Fresh installation of devel/geany started at: Sat, 02 Jan 2010 00:07:31 +0100
---> Installing 'geany-0.18' from a port (devel/geany)
This, in an age when prospective employers are known to check out candidates blogs. Entertaining.
Believing something doesn't make it true. Not believing something doesn't make it false.
There are just 3 good reasons for comments in code: (a) to refer to a published paper describing an (obscure) algorithm, eg '[fast graph traversal algorithm, M. M. Balakrishnarajan and P. Venuvanalingam ,Computers & Chemistry, Volume 19, Issue 2, June 1995, Pages 101-106] , (b) to indicate an arcane, obscure usage, which would be better eliminated, but sometimes cannot be eg in device drivers, when merely addressing a device register has side effects, (c) to very briefly document major parts of program flow-meaning, if this isn't otherwise obvious.
// XXXX I dont understand this; or # Class is defined in Foo.pm.
// increment i' and this CRAP should be avoided at all costs.
// Copyright (c) 2001-2010 Foo.Bar, all rights reserved, Licenced under the MIT license.
/* Error constants. Linux specific version.
In addition, temporary development comments help while writing or refactoring code, but they come out and become empty or (a)-(c), eg
Usually code is OVER commented, or the comments are facile eg 'i++;
The other thing to be avoided is lengthy legalese or copyright information spread all over code. If this is necessary, at all, it should fit on one line and if necessary reference a COPYRIGHT or LEGAL file
OK
BAD
-- 15 lines deleted
02111-1307 USA. */
Finally, never believe the comments, at minimum they didnt get debugged!
Early COBOL and Fortran computation code proves you wrong here. COBOL, without COMPUTE, required a separate line for each operation. Fortran from the start showed simple algebraic equations.
COBOL
======
MULTIPLY B BY B GIVING B-SQUARED.
MULTIPLY A BY C GIVING A-C.
MULTIPLY A-C BY 4 GIVING A-C-4.
MULTIPLY A BY 2 GIVING A-2.
SUBTRACT A-C-4 FROM B-SQUARED GIVING DISCR.
CALL SQRT USING DISCR GIVING RT-DISCR.
SUBTRACT B FROM RT-DISCR.
DIVIDE RT-DISCR BY A-2 GIVING ROOT-1.
Fortran
=====
R1 = (-B + SQRT(B**2 - 4*A*C))/(2*A)
Do you want 8 lines of code to write out the quadradic formula? Is that any clearer than one line? A simple one-line comment explains what *root-1* means to the program as a whole.
I've seen people write out intermediate variables like that using PHP or Python. Both languages handle that for you.
I've heard one person after another argue about how to comment and how much to comment, but what I've never seen is any kind of serious study attempting to measure what actually works best.
FWIW, in studies of the problems developers encounter in practice, one recurring difficulty is working out the motivation behind a given piece of code. This supports advice to comment why the code is written the way it is. See, for example:
Both papers were coauthored by Robert DeLine and Gina Venolia of Microsoft Research.
In LaToza, 66% agreed that "understanding the rationale behind a piece of code" was a serious problem for them.
In Ko, "Why was the code implemented this way?" was the second most frequently unsatisfied information need among the developers observed.
The reality is people might know more than you, and you are assuming that you are an uber-programmer when you are not! Maybe the programmer is more qualified than you to write Python (the language the example is in in TFA) and uses pylint. Maybe he doesn't want to sift through 272 warning messages to see which are important and which are superfluous, so he is consistent. Maybe consistency is a good thing in the software domain. Maybe he also wants every member method to be consistintly documented since docstrings are tied in memory to the object and available via help(object).
I am encouraged by most of the comments about comments here on Slashdot thus far. Most people seem to get why comments are a good thing. The author doesn't write them off completely of course, but he is not qualified to write this article either. There is just too much he doesn't yet understand, and he would do well to ask these questions of a qualified software engineer rather than posing them as rhetorical, since in a lot of cases he doesn't know the answers but mistakenly thinks he does, as I have shown.
There is a reason why language designers pretty much universally include a comment mechanism, and why that mechanism has been trending toward the docstring / formal approach away from the "willy nilly" techniques of old rather than removing the ability to comment completely.
Guns don't kill people; Physics kills people! - John Lithgow as Dick Solomon on Third Rock From The Sun
// Message below is written by an idiot, jsyk
"documenting something that needs no documentation is universally a bad idea"
-- yeah, because everyone in the world has the exact same experience and knowledge, so something that's obvious to one person is obvious to the rest of the world and vice-versa. And nobody ever changes, learns new things, or uses things that are obvious now but will be completely forgotten after five years of not using that particular construct.
This guy's opinions are worth less than the comments he eschews.
Dear Slashdot: next time you want to mess with the site, add a rich-text editor for comments.
The code which is business critical and necessarily complex gets commented a lot as a mitigation measure. But it still has problems, often because the business requirements change a lot or are poorly defined.
http://michaelsmith.id.au
I think that if more people wrote code (or, for that matter, books) like George Orwell wrote his books, code (and books) would be generally more readable: shorter sentences (lines of code) which convey a single meaning (function), strung together into short paragraphs (code blocks), chained together into a larger cohesive whole.
Complexity of that last sentence:
* 52 words,
* 17 punctuation characters,
* 5 parentheses blocks,
* at least half a dozen ideas.
But I'm just having fun. I agree with you 100%.
Believing something doesn't make it true. Not believing something doesn't make it false.
Working in accountancy (and doubly-so for audit) a well-commented file is absolutely necessary. Like (I suspect) computer code, the file should stand on it's own - a competent fellow professional should be able to fully understand it and be comfortable reaching the same conclusions. This is not only good practice for the benefit of your firm but a professional requirement: my institute comes along every few years, selects a few files and basically if they have difficulty understanding the file (or worse, reached different conclusions) then you're in a lot of trouble.
Many of the points in TFA hold up well however - explaining things unnecessarily merely adds bloat, irritates and wastes the time of the reader by telling them obvious, patronising or pointless things. And yes, a perfect accounts job doesn't require any explanation because the schedules will be self-explanatory.
The thing is though, if you're doing anything complex (earning your salary), have to make corrections (hackjobs) or judgements then you need explanations. Manager's will be reviewing your work and you or a colleague will be referring to it next year. If explanations go out of date, update them - if they had value before then value is there in updating them. Yes there is a cost, so there is a tradeoff vs their value to the file. The key is competent fellow professional. Assume your colleague will be reviewing your work and be satisfied that they will be able to understand and appreciate it.
If you're just churning out "stuff that works" then you're at best a technician.
Is very important, and it be consistent is more so; a good editor Vim or Emacs and code beautifier is your friend. Often you will need to temporarily re-format code, as you work on it, but in a big code base root-nd-branch reformats must be very rare, it screws up the SCM history, so I often re-format, fix get patch, revert, apply patch. Git etc makes this very easy.
Personally I hate the KR if (foo) {
} style. I like
{
}
An 80 column line limit is also OLD, I never edit in narrow windows >160 common.
drop, but I cant always get what I like.
I used to work for a company that didn't believe in commenting code...took ages to reverse engineer the code...
My web domain.
I believe that Knuth had volumes to say about code and documentation.
I stopped reading at this point, because the author is clearly a terrible programmer and not someone I want to take coding/commenting advice from. If you do comments right, which this guy clearly doesn't, they can save your ass repeatedly and avoid having to go to deeper, harder-to-reach documentation to make it work. Clearly not someone with any real programming experience behind this 'article' (although it looks more like a twitter feed).
If the only way you can accept an assertion is by faith, then you are conceding that it can't be taken on its own merits
I believe that some comments are good but overcommenting is messy. I prefer to do my commenting in blocks as small descriptions of what the code is indended to do.
I like to organize my subroutines and objects etc by their type. This way I can search my code for 'OBJECTS' and fine the section that describes objects or 'SUBROUTINES' and I will have made sub sections via comments 'SUBROUTINES_SEARCH' etc etc. I like few, simple, concise comments. I do a lot of work in bash for administering servers but bash scripts should be clean and properly commented just like C or PHP etc.
#!/bin/BASH
#Code specific to bash or bash compatible shells. author *ME*
# INCLUDES /etc/script.conf /usr/share/script/functions/*
include
include
# SUBROUTINES
[item1]
#workorder5678 custom;
function1;function2;#end wo5678 1/1/2007, edit 3/18/2007;
function3;end;
[item2]
function1;function2;function3;end;
#end
NOT
#this is item1. i like to put long comments that are as big as the code.
#i dont consider the fact that anyone reading these scripts in the future only
#needs to know why I did this and what it is supposed to do, not how it does it. if you dont know how it does it then you are in the wrong job
[item1]
function1;function2;function3;end;
#this is item2. it is almost like item1 above.
#I know I shouldn't reference physical locations in a file because they might move for organization but
#I thought id put a note in here that item2 references item3 with is at line 652 and item3 references item1 which is right above this on line 601.
[item2]
function1;function2;function3;end;
I think an important definition to point out is that 'comment' is not the same as 'document'. Put comments in to assist you in the future, to find code, to give a quick hint at the functions intended use so you can use that code better. Dont describe how to do a if/fi statement or how to use a function.
The author's point (near as I can tell) isn't so much "comments are bad" as "most people comment incorrectly", which I would not argue with.
Nobody who's actually worked on other people's code in the wild will argue that comments are bad (at least, nobody sane), but there IS a right and wrong way to comment. Unfortunately, while you can explain to newbies about commenting until you're blue in the face, they really won't "get it" until they've been there themselves.
Modding "-1, Troll" is not a proper response if you disagree with me. Try reason.
Well, everyone's welcome to their opinion, but it's pretty well proven after decades of software engineering that code should be commented.
From my decades of software experience across several different languages, there is no such clarity.
Comments are useful, but code is often clear enough by itself. And bad (out of date) comments are absolutely worse than no comments at all as a coder will often trust the comments first.
So while some code should be commented, not all code should be. And that's not really opinion but carefully measured analysis.
If you don't comment your code, I won't pay you for it. I'll inform the management that you neglected an important step and don't deserve a good reference.
Well that's not in the least psychotic.
I comment my code carefully when it needs it, but you are the last person on the planet I would want to work for in case I accidentally cross the invisible line that triggers your Code Rage, and certainly from past experience I know it's bad news to work for people who value procedure about quality and productivity.
"There is more worth loving than we have strength to love." - Brian Jay Stanley
put comments in places where you may later need to remind yourself what you were thinking/drinking later (or if you get run over by a bus the next guy). Also in any places where you have to code around a bug in hardware or some other chunk of software make detailed comments.
a request to programmers please have a function to dump a settings file with all the settings set to defaults
(even those that would normally not be in the settings file)
Any person using FTFY or editing my postings agrees to a US$50.00 charge
Experience shows how much you need to comment and what the comments worth writing consist of, just as experience tells you how to structure the code in the first place. "Rules" about commenting are perhaps useful starting points for the inexperienced, but with experience you can write your own rules.
Wasn't one of the mantra's of the humorous Job Security paper that someone published a while back that you should comment only the obvious stuff, eg:
while (i epsilon(n)) // increment i by 1
{
i += (j + 47) * 55 - sqrt(r) + j * j - r * r - pom();
}
i++;
Myself, I comment next to nothing unless I suspect that I won't know what I was thinking if I come back to the code in 6 months. If I come back to code in 6 months and can't remember what it does or why it does it that way, I'll put comments in for next time.
It was Brian Kernighan who pointed out that:
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."
My other car is a 1984 Nark Avenger.
While you are right (as all *experienced* programmers know, anyway) that /* increment the counter */ comments are pointless (but harmless) you've missed an extremely significant type of comments. These are the warning to future programmers why you DIDN'T use the obvious approach, or why a particular algorithm is not suitable for the task in question. Further, code that has been corrected, modified or had bugs fixed MUST have comments to identify that fact. These comments are at least as valuable as the actual code. Again, experience will teach you this.
Finally, never, ever forget the most important rule of commenting. NEVER include a URL in a comment. They change over time and cannot be relied upon to still exist in 3 months, let alone the lifetime of a software product. A similar warning should exist for book / magazine references, too. Not only do they become unobtainable or go out of print, they also get modified or corrected in the "second edition" and therefore still can't be relied on. If an algorithm has a well known name, such as Bresenham's, then by all means quote that otherwise, explain the algorithm in, or before the code that implements it. Unless you want the next guy to curse your name - provided you put that in the comments.
politicians are like babies' nappies: they should both be changed regularly and for the same reasons
4) Code must always be 'self documenting' ('would you rather use a one-liner that requires a 3-line comment, or a 10-liner that requires no comments?')
Not sure if that example supports his claim very well... After all, four lines is less than ten, and if you can adequately describe what the function should do in three lines, somebody should easily be able to flesh it out themselves if ever a need arises. I'm all for self-documenting code, but for functions that are simple enough to be implemented in one line, I say go for it. We've got deadlines, people.
This author takes full ownership and responsibility for the unpopular opinions outlined above.
4) Code must always be 'self documenting' ('would you rather use a one-liner that requires a 3-line comment, or a 10-liner that requires no comments?').
Or to put some more straw in it "would you rather use a one-liner that requires 1 character of comments, or a 1 billion-liner that requires no comments".
If your 1 line of code is so complicated that it takes 10 lines to write it out in a way that is easily understood, then 3 lines of comments is probably not going to do it justice.
Well, everyone's welcome to their opinion, but it's pretty well proven after decades of software engineering that code should be commented. The price of maintaining comment-free code is well known.
There is a school of thought among programmers who consider themselves hotshots that if you are not a hotshot you have no business touching their code. The problem with this attitude is that it has little to do with the real world, where people change jobs and programmers inherit someone else's code. If you want to write perfect, comment free code in your perfect little world, go right ahead, but don't expect to make a living at it most of the time.
It's surprising to me that someone has submitted this as a "news" item. News flash! Everything you know is wrong. Sorry I don't buy that. If you don't comment your code, I won't pay you for it. I'll inform the management that you neglected an important step and don't deserve a good reference. I won't be able to give you the benefit of the doubt when your code doesn't make perfect sense. I'll trash talk your code in front of your colleagues. Look at all the mistakes in this guy's work; I'll sure never recommend him if his resume crosses my desk. We may need to just rewrite this stuff because it's not maintainable as written.
What's really annoying is when they put comments that don't elucidate the code or their intent; they're just snide little messages from one know-it-all to another. They're too embarrassed to actually explain the code because that implies a level of insecurity they would rather not admit to. So instead they say things like: /* yeah, I don't like this either */
or
# hack, to be fixed later
Wooooo, really helpful comments there. I've seen this sort of thing countless times in my career and most others I know have as well.
Wow. You actually took the time to write that long a reply but didn't take the time to actually READ THE ARTICLE? This is how it starts:
"It seems to me getting good at writing comments is an under-appreciated part of a Programmer's development. However, I feel that this is a part of programming that's almost as important as writing code itself."
So in response to that, you write: "Everything you know is wrong" because comments are important. So is reading comprehension.
Even in the short piece that was listed it says this: "4) Code must always be 'self documenting' ('would you rather use a one-liner that requires a 3-line comment, or a 10-liner that requires no comments?'".
Now what does this mean? Hmm. Is he saying that writing 10 lines of code that doesn't need documentation is better than writing 3 lines of comments for one line of code? No. The "Myth" here would be that "Code must always be 'self documenting'". If that's a myth, then duh, he's saying code needs comments.
If you read the actual article, I'd bet that you agree with everything the author says. But where's the fun in that?
Just another bad programmer desperately trying to justify the bad job he does. I'll tell you what, you try working with a retard like this guy, and then try working with someone who "over-comments" his code, and see which is easier to work with.
- 6000 lines of Pascal
- 200 global variables
- 3 local variables
- 1 comment - the single word "midlertidig"
Oh, and one bug. Code really was less buggy back then.
Now get off my lawn, you kids.
http://xkcd.com/156/
and then on the (WHAT CLASS, Oh, I have to look where "xrgagbg" is declared) I find that "catshit" has really nice comment explaining:
When the sale amount exceeds our pre-set limit for the given plan, return a status code from the following: 12356 = DISCOUNT EXCEEDED, 62914 = DISCOUNT REALLY EXCEEDED, depending on the amount of excess as follows: blah blah blah...
When, with properly designed code I could've had:
I guess what I'm saying is is that comments will only be as good as the person writing the code no matter what. A lot of comments is usually a symptom of poorly written, unclear code.
<RANT> ***** DAMN-IT WHY DOESN'T THE "ecode" TAG WORK PROPERLY??? **** </RANT>
Because expressing an algorithm recursively provides A LOT of clarity when the operation is in fact recursive. A proper language (like Scheme) that implements proper tail recursion this works really well. For other languages, its a fail due to the inability of the system to perform the calculation in constant space (due to pushing stack frames). That being said, most languages (including C, C++, Java) *could* be made to do proper tail recursion, then that wouldn't be an issue and you would be able to have the clarity of expressing a recursively solved problem with a recursive algorithm. Blame the problem on poorly written compilers and poorly implemented languages.
Over-the-top Response Guy! Giving "Over-the-Top Responses" since 1970.
There is another myth that says just because something is mathematically unambiguous it also intuitively obvious. And yet the real implications may be lost in the complexity of the situation or problem. Comments can point out anything useful to the code reader.
Also, if you spend enough time staring at a piece of code you can usually figure out what it does. But that does not at all mean you can figure out what the programmer was really trying to do. His/her internt may have been quite different. You can also not figure out all the possibly erroneous assumptions made about the problem space at the time of coding.
Comments form a useful redundancy and sanity check. Being cutely terse in comments is as bad as the clever but incomprehensible code one liner.
You should add comments anywhere you can predict a reader may have a high probability of misunderstanding.
- Tom
Proper commenting is a blessing, not a curse. But I have noticed over the years that many coders need to learn proper grammar and spelling.
“This are a funktion to comput the valu of pie”
Unless it is an incredibly complex method (that should probably be broken up anyway) then the name of the method (in conjunction with the name of the class) as well as the names and types of the arguements, return types, and exceptions thrown, should be sufficient to tell you EVERYTHING you need to know. If not, you're not doing it right.
Over-the-top Response Guy! Giving "Over-the-Top Responses" since 1970.
I had a real asshole of a CS professor in college (who will remain nameless) who insisted that every single line of code be commented. He took 10 points off an assignment once because I didn't comment a line that only had a closed brace on it.
And here are the missing comments:
Did you fully understand the code by itself? Are any of these comments harmful? Would anyone seriously rather have the code without the comments?
Hopefully the Internet Archive and Google will fix the 'out of print' problem.
Programmers, who cant, should not touch complex code, and NO amount of commenting stops them (fixing) it from right to wrong. If you dont know you should not fix code cos it looks wrong, I would cut your coding finger off. If you make any change to a complex algorithm you have to review/debug it ab initio.
Read a lot of Linus (Tovolds) comments about code, reading source, and what NOT to do and you will learn a lot. If you want to be a good programmer follow good examples eg Prof Sir Peter Swinerton-Dyer, Prof Tony Brooker, Dr Martin Richards, Edsker Dijkstra, Prof Don Knuth, Marc Andreeson, Linus Tovolds (for a few examples). A really good example is the early evolution of Git, when Linus was the prime implementor, it was awsome.
By reading good code you lear a lot, but if you ambition is to be a typically under-educated US code-droid, dont bother. Code, like lierature, should not be limited by essentially illiterate readers,
I do like your sig though, very true.
There's a quote in Code Complete ( I think ) that one should be able to read code in the evening in one's armchair with a glass of brandy as if it were a novel. My personal version of it - and the criteria I impose on my team - is that I should be able to read and understand the code w/o having to think too hard or knowing much about what the programmer is trying to do. It's really easy to enforce this in code review - if I can sit at someone's desk and quietly read their code and know what's going on, it's good enough - either sufficiently-commented or self-commenting. On the other hand, if I have to ask what a section does, whatever the developer says to me verbally is also a good candidate for a comment. The other key factor in commenting is that it must be clear not only what the code is doing, but why. For example, if I encounter the following in the code: if (productType != SOME_TYPE and client != SOME_CLIENT) { sendReportToCleint() } it doesn't help to have a comment saying // Send the report, unless it's SOME_CLIENT buying SOME_TYPE.
I already know that... what helps is // Don't send reports to SOME_CLIENT for SOME_TYPE because their system // crashes on this message and they don't know how to fix it. This is // as per discussion with Jon Doe on mm/dd/yyyy
This is basically the principle of least astonishment
http://ed.markovich.googlepages.com
I actually comment the main parts of my code before I write the code. Reason being is I like to get the general layout of the code down first and then add further comments as I go along to explain my reasoning *to me*.
I don't know how long it's going to be before I come back to a piece of code and nothing annoys me more than having mental clutter in my mind. I don't want to have to *remember* what why or how certain bit of code does what it does. I just want to pull up my code and explain it to myself and say "thanks me of the past for considering me today". Coders that don't explain what they are doing in their code are just hard to work with.
Well commented code helps code look beautiful and flow like poetry. When you look at code you can see almost immediately if it is going to be painful to modify. I try to put as much back onto the machine as I can, like using database design tools so I don't have to keep stuff in my mind.
I use Nassi-Shneiderman mixed with pseudo-code on paper to do a design (I'm getting better at UML tools - so soon I hope to upgrade this part of my coding practise) and when I'm happy with that I move to comment before writing the actual functional parts of code, increasing the detail of the comments as I go along. I used to be shy about my code but now I'd be embarrassed if I didn't comment my code - because it's not polite to my colleagues. Comments mean I don't have to waste time trying to remember where I was up to as it's simple for me to compare it with my design and away I go.
My ism, it's full of beliefs.
Isn't the lack of comments, it's getting lost in a dozen (or two or fifteen dozen) different source code files, trying to figure out what the original programmer wanted to do. F/OSS is a beautiful, wonderful thing, and I'm fully committed to it, but I can't tell you how many times I've downloaded a F/OSS project with the intention to tinker with the code, only to get completely lost in it.
/* see doodat.h */" and "function( SOMEVALUE ); /* see heredat.h for SOMEVALUE */" because I know that even I, the guy who wrote the stuff, will come back years later and be totally lost.
:)
Just for record, I'm a commenter, and I like comments that explain what the code is doing. But to help address the aforementioned, my code contains lines like "SomeFunction()
If I had a dollar for every time I've grep'd for the main() function, then followed the execution thread just to see what a program was really doing, I could take us all to dinner.
Cogito, igitur comedam pizza.
you've obviously not heard of The Commentator ...
language: it's self-documenting code.
I've seen some well commented projects where the code comments were really the only thing you could read to get a handle on the business logic behind the application. Occasionally you'll read a comment and realize that the original programmer had a misconception about how the next bit was actually supposed to work. Maintenance programming is as much about understanding the state of mind of the guy who wrote it originally as it is about writing code.
I'm trying to teach myself to set people on fire with my mind... Is it hot in here?
As others have said, if the comments are along the lines of
then it's just complete rubbish.
What I would like to see though, is some very complicated encryption algorithm with and without useful comments (useful being the why). See which of them are easier for the average developer to get a grasp of.
I have a unique system for code comments that serves my own projects incredibly well.
I find that comments break the flow and readability of the code, especially when there are huge blocks of comment for a small section of code.
What I've done is set the tab size to 85 characters in my editors. I do not use tabs (one because I prefer 2-space indenting, two because most editors have Smart Tab features), so to comment a line, I tab at the end of the line and add as long as a comment as I want about the line there.
On a widescreen monitor, this makes both code and comments visible (up to 90 characters of comment on my 1440-wide screen), without one breaking the readability of the other.
Some other developers I've worked with have liked the system so much they started using it as well.
that's a whole lot of free-floating ad-hominum based on
the shaky generalization that every class or public method
needs a comment.
Clean code tells you how, good comments tell you why.
Fixing the "how" becomes significantly easier when you know "why" the code was there in the first place.
Why is this so hard to teach developers? My last 3 projects had minimum documentation standards that developers always had trouble with. They would just add comments that said what the code did. We usually don't want comments that describe what the code does. We want to know why the code does what it does did that. "bug report foo said the exception exists because" or "Law X in state YY requires this behavior different from other states".
The exception is of course the hot shot developer who does all kinds optimizations or algorithms beyond the reach of most of the team. We can't always stop them from putting that code in so we force them to describe what it does.
I do mostly C# + SQL, and have gotten into using StyleCop since it came out. It forces you to use xml comments on every method, field, property, public or private. For properties you have to use "Gets or sets the value of Foo" or "Gets or sets a value indicating Foo" (for bool). Since the only duplicate comment allowed is "The parameter is not allowed," what I do is fill in by default ALL the comment parts (summary, param, returns) with "The parameter is not used."
Then I get my code to be StyleCop clean, and go back and search for "The parameter is not used." I update the comments appropriately.
For comments in the body I use "// TODO refactor" or something terse. I find putting SOME comment block on everything makes it nice and readable, and if I see a huge block of stuff with "The parameter is not used." I know it's something that's not important to comment (like a million TextBox field declarations), or else it's somethign important I haven't gotten around to commenting yet.
So this is working all well for me :-) The gist is I use the StyleCop guidelines, a boilerplate for "no comment comments", and terse inline things usually indicating work items in the body.
Works for me and I explain it to the next guy and he can figure out my intentions. Right balance of anal retentiveness and terseness for me.
I had this discussion with some mainframe folks a couple weeks back. They never rewrite code. They don't understand what all the code does so they always wrap existing code with new code when adding functionality. Sometimes the new code just stops the old code from executing in certain situations.
.. and also, you are correct. With code that needs to be deciphered, you're generally better off re-writing it.
When your public method is named something like initCamera( int camID ), or grabFrame(), I'm not altogether certain what's gained by spelling that out in a comment... maybe if there's something deeper going on, but if it is as simple as it looks, why pollute it with additional words to read that convey the exact same meaning?
Because his opinion is against a fairly standard convention he declares himself right and the opposite opinion to be myth.
How arrogant can you get. Really.
"I think MS Windows sucks Donkey Nuggets. Therefore everyone should switch to Android. The power I wield is frightening."
I've been writing code for over 20 years. One thing you learn is that you don't want those "Hey how do you do xyz with method ABC in the code you wrote 10 years ago." calls. How do you avoid this your code follows you like a bad poo cling on. YOU COMMENT YOUR CODE. With descriptions, examples, purpose and anything else that is relevant. You probably should spend as much if not more time on the comments than the code. Cause it will save you heaps of time in the future.
As for documentation that is external to the code? Well it's useless. Because repositories change move and mutate. Over the years the design/requirements/arch docs all seem to vanish. The only thing that seems to live on is the actual code. Why. The code is the business not the power point slide show. The code therefore should also be the documentation.
So Mr. I'm 20something and know everything about writing code. Time to join the real world. I for one never want to have to fix your code.
"In most cases" That's the bottom line - just apply the same rule to comments you do to code; overly verbose is usually more confusing.
"It's a doughnut stuffed with M&M's. That way when you finish the doughnut, you don't have to eat any M&M's."
... The other thing to be avoided is lengthy legalese or copyright information spread all over code. If this is necessary, at all, it should fit on one line and if necessary reference a COPYRIGHT or LEGAL file ...
You know....for many of us developers, this legalese is dictated by company lawyers. We can't simply "avoid" putting this legalese in our code.
Unless I'm coding some high speed spASM code... In regular C/C++ I stick to commenting a whole block as opposed to each line. I've worked with overly anal coders who code each line.. I want to kick them in the teeth.
Comment removed based on user account deletion
I hate uncommented code, I hate it with a passion. I hate having to track down how this or that works with no comments or documentation to figure something out. I hate hearing people say, "The code is the documentation!" because I just want to slap them silly.
I comment my code, whenever I make changes I damn well do my best to document those in the code and developer guides I usually write for projects. I figure if nobody is going to be kind enough to me and leave good documentation and code comments then I should set an example. Thankfully at my current job, the last developer to retire left such good documentation I'm in heaven and even tried to clean up my documenting methods more based on his work.
~~ Behold the flying cow with a rail gun! ~~
I like the comparison to literature and how it illustrates personal preferences, but respectfully disagree with the idea that literature will all eventually converge into very short, efficient, single purpose works. The entire point of literature is to convey some theme(s) through the medium of a story. Stories are not meant to be efficient or singular in purpose or meaning, necessarily. When you read a book, you are along for whatever ride the author wishes to take you on. And it is exactly that ride, not some re-factored or boiled down version, that he/she wants to experience. If you've ever read Cliffnotes instead of an original text, you'll understand. I don't think all literature needs to be more "readable", sometimes it's ok for something to be a little beyond your comfort threshold, it makes your brain work just a little harder, and I think that's a good thing. Unless of course you want all books to read like Green Eggs and Ham :-)
I would rather use code with 1 line of high-level language, requiring three lines of comments, than 10 lines of high-level language code, with no comments, because 99% of the time, the MACHINE is what's going to be reading the code, in compiled binary form. At compilation time, as I think everyone on /. knows, (or should) all those comments get sent Express Mail to /dev/null, and the result is usually more compact final-product machine code, which executes faster, using less time and processor power. The other 1% of the time, when it is I who am actually looking at it, I don't mind reading a few sentences from the previous coder telling me what he was TRYING to accomplish with that function.
Lots of comments doesn't make bad code good.
For heaven's sake, just read:
Kernighan, B. W. and Plauger P. J. (1978) The Elements of Programming Style, 2nd edn. McGraw-Hill
They deal with commenting and its pitfalls perfectly adequately.
Object-oriented languages in general require less documentation since good design and properly named methods and properties do document things relatively well.
I think it's the object-oriented program structure, not language features, that makes things easier to understand. And only where that structure makes sense.
For example, have a look at Simon Tatham's puzzle collection. It's object oriented C code, with excellent developer documentation.
There's an interface "Game", implemented by Sudoku, Minesweeper, etc., which basically defines all of the game.
This interface and the interfaces it can talk to are all documented outside of the code. Each of the implementations have comments in the code explaining the implementation.
Class-level documentation is much more important than documenting the details of the methods themselves.
More important---they're more useful towards which end, exactly?
You bring up Java. I find that in java, whenever you want to pass around a method, you have to duct tape it onto a class and instantiate that. See for example Comparator; really you just want to pass a comparison function to the sort method.
This tends to introduce "spurious classes"; classes that you have to create because of constraints in your language, not because they're the logical way to organize your program.
In those cases, I think it's much more important to have good higher-level architecture documentation; "these are the classes involved in setting up a network connection. Here's how they relate. Here's the order in which you instantiate them, and the methods for doing so."
I think this springs out of a good usability idea: have the user's (programmer's) task in mind. Instead of documenting the tools `find' and `grep' and `locate', document the process of searching for your stuff.
Instead of documenting the Socket and Packet classes, document the process of shoving bits onto the wire(lesse)s.
Perl and C code, on the other hand [vs. Java and .NET], can be unmaintainable even with a number of comments, because the old functional design is not easily maintainable.
Again, it's program structure, not language features. You can implement most structures in most languages (except maybe B&D Pascal, but hasn't Pascal become less B&D over the years? I haven't kept up).
A minor nit: ML/Haskell/Lisp programmers might not agree with your choice of the word "Functional". They'd call the paradigm procedural; I don't know if there's consensus about which word to use for the typical designs, if there are typical designs. In any case, there's no "The" way to design C/Perl programs. Oh, and perl is OO nowadays.
Consider rewriting these in a more modern language.
Consider understanding the capabilities of the less modern languages and the restrictions of the more modern languages.
I'd instead recommend refactoring the code instead of changing languages. It seems like something you could do faster and cheaper. Hey, I just saved our department more money than you did :P
- 1 comment - the single word "midlertidig"
FWIW, "midlertidig" means temporary. My guess is that it wasn't used for a local variable in a `swap' method ;)
that the people against commenting sound like they've worked in environments where they were forced to write comments
and the pro commenting people sound like they've been struggling through some poorly commented and hard to read stuff.
It's about balance. //increment x)
Some comments help a lot to make code more intelligible.
Some is pointless noise.(ie
A lot would not be necessary at all if the code was sensibly structured, with good variable names
Software development is a broad enough topic to make this discussion difficult without examples.
http://code.google.com/p/virtualagc/source/browse/trunk/Comanche055/CM_BODY_ATTITUDE.s?r=258
http://c2.com/cgi/wiki?CommentExample
'would you rather use a one-liner that requires a 3-line comment, or a 10-liner that requires no comments?'
What defines 'requires no comments'? To me a comment is useful if it saves someone (me or anyone else) time later. Even if you have a brilliant and fairly self-documenting 10-liner, what is quicker for a programmer: reading a one line comment or reading 10 lines of code? What about a poor scripter that is using a c/c++ function from a simpler language that may not be able to decipher the original code at all, no matter how 'self-documenting'.
Is it ok to call initCamera possibly multiple times, because I don't know if it is inited already?
If not, how do I tell if the camera is initialized?
What is a frame? What is a frame a part of? Where did I grab it from? Where is the data
that I presumeably grabbed this frameworth of. i.e. where did I grab it to? In what format is it
represented? How much processing does it take to grab a frame? A trivial amount? Or can
I have my code call grabFrame 1000 times a second?
I guess that stuff was all obvious from the names.
Where are we going and why are we in a handbasket?
IPreferToUseDescriptiveButLongIdentifiersInstead(ForBothFunctionsAndVariables);
YesInCaseYouDidntGuessILike(Java,CSharp);
Don Knuth described Literate Programming in which the program is embedded in comments, one using a preprocessor to write the program. The preprocessor runs as a command processor, using the source as a batch file. One gets a properly sorted batch file as output. Because of this, one can overcome limitations of the programming language, use pre-processor variables etc, and produce several linked files etc.
Documentation is relatively straight forward, since one writes what one wants to get, limitations, API and examples by way of a wish-list, and then create the routines that make this happen. You can write a tight section like "File I/O", for fileopen() and fileclose() along with getline(), putline(), etc, without exposing the filenames to all of the subroutines. I've written a program to write Web pages in it, for example, http:\\www.os2fan2.com\pgloss\index.html and its attendent pages. It's pretty elegant really.
The temptation of LP is to use it to solve a problem per source, a note that Jon Bentley (Pearls of Programming) noted. LP is about sorting problems: however, the sort of problems that Knuth sought to solve are complete things in an educational setting, not bits of problems in the real world. None the less, I use it to write terse batch files etc, where the LP source is the help file too.
OS/2 - because choice is a terrible thing to waste.
One of the places I worked at extended the "no comments" rule to "no Javadoc, no documentation except on the Wiki". Of course, once the code is passed to another company the Wiki won't be (since it was blocked from customer sight it will be considered "company confidential") and there will be absolutely nothing to tell the next group how to change anything. The same project engineering head said that since this is an Agile project there should be no design documentation (at all).
The only answer for this is "comments are evil". I gave up and removed my comments (he typically changed code without consulting the programmers anyways).
This is what the "comments are evil" line gets you if handed to morons.
We have to meet formal documentation requirements. (FIPS 140-2, Common Criteria).
Doxygen makes that a breeze - the documentation in the source isn't particularly readable (but not useless either), being able to annotate the sources and produce the documentation you have to have with the push of a button is great.
It has improved code quality and found a few bugs as well, but that's a long term gain not short term -i.e. if the call tree's look like spaghetti it's probably clean up time.
All in all - taking the long view (several years), it's been a massive gain. Short term, it's a looser - it eats time putting good docs in place - so for most software companies - where "push it out the door" seems to be the rule, you aren't going to be winning.
I've worked on both ends of the spectrum.
In my first job out of college, the boss was an old school C programmer who was beyond hardcore about his coding standard. The code not only had to be commented (with a comment on EVERY SINGLE LINE), but also properly formatted. What's the difference between:
}//for numPoints
and
} // for numPoints
If you said, "Having the boss yell at you for five minutes." you win the prize. I'm not kidding or exaggerating.
-----
My current job is very lax. If the code works, it doesn't even matter if it's been run through an obfuscater.
Of course, if something is wrong with the site, then I (or my co-worker) better be able to fix it -- quickly.
So, either write it 100% perfect the first time, or keep the non-obfuscated code around; keeping the site working as expected is the *only* thing that matters.
-----
As a software engineer, I prefer the rigor of the first. (I can do without the yelling, though.)
As a code monkey, I prefer the pragmatism of the second.
Maybe my next job will put me somewhere in the middle.
As someone who frequently gets asked to work with code written elsewhere, adding features, fixing bugs etc, I find comments in code mostly useless. Reading and understanding the code is the only way to really understand what is going on. I find the comments don't really save time, as if there is a bug in the code, the original programmer did not understand the code anyway.
Every line should be commented, like: // Declare function called doit with one int param that returns an int // See above comment // The function's open brace. I like to put braces on their own line. You should too!! BTW, this is C code, so braces are totally the way to go.
int doit(int i)
{
if(i == 0) // Check if i is 0. You know, in C, "==" is the way to compare values, unlike in VB where you use a single "=". Just thought u should know.
return 0; // Return 0. That is, all the bits of the return value are 0. We could also return i, because i is 0, too. That is, all the bits of i are 0. On a 32 bit system, there would be, like, 32 0's.
else // <-- But you forgot to comment the meaning of this line!!! Please redo and resubmit to code review
{ // Begin an if block using a brace (this is C syntax!!!)
int j = i - 1; // Declare an int variable named j that is one less than i
return i + doit(j); // Return the sum of i and the value of calling doit with j
} // Finish the if block with a C close brace. By the way, we could have written the above code as return i + doit(i - 1) without using the braces. // The function's close brace.
}
There! Now that is both way readable and informative. Anything less would just not pass my code review.
Ever stop to think
"Comments aren't actually executable code, therefore they aren't maintenance costs, right?"
// cache result.
Absolutely right, yes. Although comments also need to be maintained when the code is updated, their maintenance costs are negative, because they SAVE TIME (That's why they were invented)!
The same goes for other forms of documentation- some code would not only take *more* time to maintain without documentation, but would simply be impossible to maintain without it.
"Comments make code more readable."
Yes. Not all of them, but when used properly, they damn well do. Example: If there is a 'magic number' in the code, a comment can help explain how the author arrived at that magic number, and allows the next developer to change the magic number to another value as needed. Without the comment, this would be impossible.
Another example:
unsigned long lastsectornum()
{
if (!(this->foundlastsectornum))
{
this->m_lastsectornum
=lookuplastsectornum();
this->foundlastsectornum=true;
}
return this->m_lastsectornum;
}
I'd say in the above example, the two-word comment makes it a lot more clear what is going on.
"You should comment every function, method, class, and module"
Jason goes on to use this as an example:
def get_x(self):
"""
This method gets x.
"""
That's simply a bad comment, but it doesn't invalidate the statement. We still want a comment there- one that explains what 'x' is. Without comment, there is no way to tell. The problem here is not that there *is* a comment, but that the author (in this case Jason) wrote one without added value. For any function with parameters, however, we'd want to know what upper/boundaries they have.
def get_x(self):
"""
Returns horizontal pos. of the mouse cursor
"""
See, this is much clearer! (though arguably, the method name is wrong and the comment unnecessary). There's still a use for a comment here: What does the method return if no mouse is present? Is it the responsibility of the method to check for this, or is it simply not allowed to call the method in such cases? Comments to the rescue once again.
"Code must always be self-documenting"
Jason calls this a myth as well, and this time I have to agree with him. Although we should strive to make our code as clear as possible, it *can* not always be self-documenting. Comments were invented to solve that (see above).
However, "In most cases, comments should be viewed as code smells." is utter bull. Jason should spend a few years writing assembly language. Perhaps then he'll learn to value comments for what they're worth. Personally, I have a strong feeling I wouldn't want to maintain Jason's code, as he feels that he need neither document his code nor write self-documenting code.
Visit http://ringbreak.dnd.utwente.nl/~mrjb/growingbettersoftware to download your free copy of the book
Self documenting code is a desirable side effect of a clean coding practice that has something more in mind. Writing very 'verbose' code is like drawing a precise picture of the analysis of the problem the code is supposed to solve. It 'tells you a story' of divide and conquer. This serves you so much when it comes to bug fixing or more important: bug preventing. And it gives you an extra level of reflecting about what you're actually doing.
To many programmers such a discipline may seem like talking to little children and explaining each and every bit of obvious information. I had the experience to get bad grades in my math tests because i thought myself to have good memorizing abilities so i preferred not to note too much intermediate results. That's where I discovered my psychology of introducing bugs.
/* ... // Crappy code here
* Sorry, had a couple of beers and no time to fix it, yet
*/
Since I'm using remarks in my code, my coding has increased double in speed.
for loops, a missing bracket can be easy defeated by adding a code remark behind every closing bracket.
In big sources, this is a convenience to see where exactly a subroutine has finished to its result.
Not only for loops, but also repetitive code, regexpes (which you can write in more than one way) and parameters will give a lot more meaning when having a little bit of documentation; even, if not for the next programmer.
I could not imagine an architect or anyone who builds buildings would be not making *any* sidenotes about their project.
--- I am known for the ones who want to find me on the net. Is that a privacy risk or a privilege? One might wonder..
I kinda know what you mean, but there's been times where I've been working in a block of code, written by someone else, where a short, very-high level comment about what the code, in general, was *supposed* to be be doing, would have helped out a little bit. But, you say, can't you tell what the code was supposed to be doing from a) the function name, and b) the code itself? Well, IIRC, the function was ambiguously named, and the code was broken - the code wasn't even correct, hence the reason I was working in that block of code, trying to fix it, but it wasn't clear what the code *should* have been doing, from a high level.
I mean, I still wasn't completely stuck. I knew the code wasn't working, and how it was effecting the rest of the program, so I *was* able to kind of work backwards from the rest of the program and make a good guess what the code probably should do, and just kind of worked from there to get the desired results that would make the rest of the program work. But, a one or two line comment from the original code author might have helped save some time.
The only Jason Baker of note is a football(American) punter.
So are the submitter and the author the same person and this is just a 15 seconds of fame?
When in doubt, optimize code for future maintainability and legibility. Hardware gets faster. Programmers don't.
There is an important caveat to this -- although it might be obvious. You *always* design and code with awareness asymptotic time complexity, and sometimes space complexity. If you just think "optimize later", then you might be doing a root canal on your code.
I only say this because there are so many developers who have no idea about how to use algorithms and data structures.
Like all pain, suffering is a signal that something isn't right
.
The reality is that there are good comments and there are bad comments. Good comments are helpful, bad comments are not. I forgot to mention that there is a third type of comment, the evil ones. The evil comments are wrong, leading subsequent programmers astray.
Two words: "Code Completion". Many IDE's (like Netbeans) use PHPDoc comments to make educated guesses about code completion. A one-liner function with 5 lines of comments is worth it.
My employer has a very large, and, IMO very organized, but incomprehensibly so, codebase. There aren't a lot of high-level specs, and there aren't a lot of good comments in the code. There are a lot of times where I will be tasked with finding a bug, and I can see where the program breaks as well as why it breaks, but without knowing what it's supposed to do, it's hard for me to make the right fix. There are a lot of places where we have some 1500-line (don't even get me started on this) C++ function handling some enormous data structures (20+ member variables, of which roughly half of those are equally-large data structures) and there's basically no info on what any of it is really supposed to do (other than whatever function/variable names there are).
Whenever I'm not cleaning up other peoples' messes, I tend to focus on making sure it's obvious what I was trying to do and that the input/output of my function is well-specified, so if someone comes along behind me it's easier for them to locate the "true" problem by looking at the specifications than just finding a "problem" and trying to make sure it doesn't happen again with some quick fix. I also try to keep my functions as task-specific as possible and small enough to be somewhat comprehensible on their own while considering that we don't have unlimited stack space on our devices.
But, then again, I'm relatively new there. They might break my will eventually...
Very clean and easy code ... No comments required ... Obvious ...
Okay then, without looking this one up on google or fishing from your CS books etc. Please explain this simple piece of code.
int n = (count + 7) / 8;
switch (count % 8) {
case 0: do { *to = *from++;
case 7: *to = *from++;
case 6: *to = *from++;
case 5: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while (--n > 0);
Also some very nice ways of counting bits in a variable, traversing lists, sorting arrays etc. Very neat and clean code samples can be found on the internet I challenge every one to explain some of the 'cool' samples you find without reading any comments on it.
So here is the deal:
Professional code base == years of development and tons of business logic from tens or hundreds of different developers with > 1 million lines of multi-threaded code. So you're telling me that all you need is to look at the code base and you immediately understand the full extend of every line of code you read ?
Have you ever looked at code from India or at code from a freshmen ? Have you ever tried to re-factor functions with 6000 lines of code or more and source files with > 60.000 lines of code with directories of more then 500 source code files ?
If this is so and you can naturally fix all bugs in these multi-threaded morass of code, I would like to interview you for a position at our company and am willing to pay you more then any other offer you currently have.
You are talking about hypothetical code that does not exist in the real world unless the code base was written by only one person. And even then. Point in case, we have a cool library that is at the core of our multi-threaded application base. Written by one guy. No comments. Very clever, dense code. So clever in fact, it is unreadable for any one but this guy. So lets assume he is mortal and / or he decides to leave the company at some point in time. I am sure you have no issues to re-write the code base that is at the center of a whole companies product line and makes the product stand out from any one else's product. Or you have no problems figuring out all the bit shifting etc that is taking place with mutex's and pthread calls all over the place in no time at all and fix problems in a matter of minutes.
BTW, the same guys who say source code comments are useless also ignore compiler warnings and are the same type of people who insist they can re-write a whole application suite in a matter of weeks that has taken a team of 10 multiple years to write ( usually because they are too lazy to read and understand the code ).
I could go on, but I am so upset with this article I better stop here.
A project is not just about the codebase anymore: now we've got issue tracking
systems and (D)VCSes ruling the day.
Seeing your code as something independent is foolish for anything but the
smallest toy-ish project.
I like something like:
bugfix for issue ${your-favorite-bug-tracking-system-bug-number here}
or even:
bugfix for issue ${your-favorite-bug-tracking-system-bug-number here} introduced in changeset 5b8362e108fc
Should you ever change from (D)VCS (Mercurial here), you can write a script to automatically update
all the changesets reference.
That's how they do it in "Real-World[TM]" millions-and-millions line of code projects
where you need both the code and references to discussions / bugs description
concerning said code.
The OpenJDK comes to mind.
Millions and millions of lines, I tell ya, you should listen kids ;)
I work in an environment with 30 developers, 400K SLOC and 2 global sites specifically for one component (in a family of 12). Our review process is pre-submit and done through e-mail. Reviewers are as responsible as the code submitter in identifying negligent comments. From the check-in comment, the intent of the change should be obvious; the comments need only support the intent. Asking someone to review a change before submission has the added benefit of ensuring at least two minds understand it (even if one is only cursorily). If you don't understand the code, you ask questions and ensure each question answered results in a architectural document change, code change, comment change, or at the very least, check-in comment change. Sometimes, it is not apparent to the submitter that his concepts parlayed into code are consumable by others. It's for that reason it is important to put in bad code sometimes to see if people are paying attention.
---- The geek shall inherit the Earth.
Well, all I can say is that "Empty vessels make the most noise."
...when you wrote 3 lines of code that required well-deserved and richly written 20 lines of comment to explain how they achieve their purpose, and then a change to project makes them obsolete and unnecessary?
OTOH there's still a piece in my major project // uchar reverse_det_ndx[MAX_PROG][MAX_FAZ][MAX_DET][MAX_DPF+1]; // TODO: Recall, what did I need this for.
45 5F E1 04 22 CA 29 C4 93 3F 95 05 2B 79 2A B2
code:
for(unsigned char x=limit; x>=limit; x--) // no comments whatsoever
{
do_something_with(x);
}
Direct result: my fingers tightly locked around throat of programmer who wrote that.
Indirect result:from then on, whenever he purposely uses variable overflow/underflow, he always comments that he does.
45 5F E1 04 22 CA 29 C4 93 3F 95 05 2B 79 2A B2
Best book on coding is Code Complete. Also has the best advice on comments. // open filename
If the variables and functions are named AND USED correctly and
you don't comment on standard procedures open(fname)
then there is very little that needs commenting.
It needed to be said.
In my long career I've never heard anyone advance the first two arguments, and only one manager ever required we comment every function. This guy must have just had a difficult time finding something to blog about if this is all he can post. Can you guys posting this do some vetting? Please?
-- Programming with boost is like building a house with lego. It's a cool but I wouldn't want to live in it
I'm sorry, but that's not a myth. I'm sure, like all things, it depends upon time and place, but I assure you that the scanline hidden surface removal code I recently wrote that uses a span based methodology is entirely dependent upon good commenting so that debugging can involve someone other than myself. There aren't many software rendering specialists out there anymore. My code has excellent naming conventions, is organized clearly, but if you don't understand all the factors involved it would be laborious even for myself to debug this a year from now. Documenting the edge cases, the oddities, the factors other design decisions elsewhere in the system have made to impact choices in this code, it critically important.
Hopefully it is commented thoroughly enough that someone reasonably proficient in Java and 3D could understand why things work they way they do. All of this is, of course, also included in a design overview document for this aspect of the system, but documents like that tend to get lost as time goes by. Documenting in the code why exactly we, for example, interpolate colors the way we do and then clamp them to an applicable range in critical to avoid someone messing up overbright lighting.
Now, of course, this need doesn't apply in all scenarios. I don't heavily comment the model loader's methods (although they are well commented) to explain how we manage endian-ness and why, although I do mention to beware of this issue, but that's because in 3D this is a fairly common thing to have to support; ergo, I expect a developer with 3D experience to understand basic issues with modeling formats (if not the specifics of a particular format.)
Loading...
noone seems to have remembered the burden caused by over zealous commenting in interpreted languages as opposed to compiled where they comfortably vanish and save otherwise wasted clocktime.
It always amazes me that management requires fully commented code and complete documentation, but they don't allow the time necessary to create it. You have to allow at least double the amount of time that you would give for coding because you are effectively writing the program twice. When time gets short, the first thing to go is the documentation. Your users don't give an unwashed rats ass about how well the code is commented. They care about whether or not it does what they want. Therefore, it's entirely up to management to allow for time in the project for documentation and commenting code.
It's a very dark ride.
1) Comments have a cost ("When you update the code that the comment references, you usually have to update the comment as well")
// this fixes defect 12345, by ensuring list order validity:
Worst case, sure. But the future maintenance cost of having no comments, or wrong comments, is higher than the cost of no comments. For example, if you code to an interface, and the interface does not specify what it does, then you have to go look at how it's implemented, which takes a lot more time. You might then say, well if the interface is wrongly documented... but then you're begging the question. Interfaces are contracts, and contracts require precise definitions.
2) Comments make code less readable
Wrong comments make code less readable, sure. But take a simple example:
insertPositionalMarker(context);
Why does this routine get called? What happens if I change it? Contrast with:
insertPositionalMarker(list);
Woohoo, now I know why it's there, and if I later need to understand the context, I can easily back reference to the original bug, and if I feel the need to make changes, I can regress the defect.
3) There is no need to document something that is obvious, and documenting same is a bad idea.
Maybe, if you are a genius and know (or want to frequently review) every line of code. In the real world, however, we call other people's modules, and we use context sensitive editors that hint a range of possible calls, including overloads. Which one do you pick and why? Does the call have side effects? A concise description of the interface contract is imperative for productivity. Not having documentation requires asking them, or wading through their code. Either one is a waste of time.
The fourth one in the article, code must always be self documenting, is correct, but trivial. Some code constructs are not obvious or readable, because they are complicated -- e.g. written by a domain expert.
The original article is fine for simplistic code bases, but doesn't work in the real world of high productivity, large scale applications. Anyone who doesn't comment their code is trading a small amount of their time for a large amount of everyone else's, and that's not scalable or efficient. In fact, it's just rude.
Join the window installer's union, where prosperity is a brick throw away!
Slightly off-topic : what annoys me most is being asked to use very verbose variable names (think 20+ characters), because it supposedly makes it more readable. That's utter bullshit, and the people who ask me this never did anything very complicated. It's much harder to juggle with a bunch of long variables than to juggle with two-letter variables. I prefer to use short variable names and comment them in a block at the beginning of the function.
That's the very reason why mathematicians use one-letter 'variable' names. Think about it.
You just got troll'd!
I think that code should contain no comments at all.
Notice that I did not say that code should not be commented.
The problem here is that we are trying to do two things together, where we should be doing them separately. I'm envisioning a development environment that gives you control over your comments - you don't put them in the code - they live separately and are anchored to the code. They could be viewed separately (or together with the code), expanded and collapsed, and nested for varying levels of explanation.
Comments today are second class citizens, but we should be thinking about providing a full set of features around the commenting of our code,
Bravo! Surprising that you have achieved so much insight in your short career of two or three high school projects, congratulations!
Uncommented code is very often shitty code - and so is over-commented code.
Uncommented code is good if you 1) will be the only person to ever, ever, ever touch the code, 2) you have perfect memory, and 3) you don't expect anyone else on the planet to ever use it.
General rule - assume the person following you is *not* a uber-coding-adept but just some average-joe/jane code-mechanic who has far, far more work on their plate then they can handle already. And this isn't a freaking competition - you **are** allowed to give them a hand in understanding your latest convoluted masterpiece. You usually are on the same team.
At my workplace they have a library of perl code. Most of it is pretty good _code_ and do usefull function. But there is very, very, little comments and the writers simply LOVED to use the latest arcane features of Perl. No subs are documented - "Hey - read the code!".
Sorry but why should I have to read and try to comprehend your convoluted code just to use it? Its *your* responsibility as the author to make sure it does its job and its also *your* responsibility to document (and maintain that documentation) the interface to your code if you expect anyone else to use it. Its *my* responsiblity to follow your documentation and use your code properly.
Also subroutine/module/method comments are often picked up by IDEs to provide on-the-fly documentation (or 'hover-help') for anyone using your code - I'll admit that I find this very useful.
On the Other Hand - there are those who grossly over-comment their code. We have a bunch of these types of people as well.
They Comment every single change and bracket their changes with comments showing the Change Request number as in :
// FR: 1203938-
The results, over time, a patch work of these comments - overlapping each other.... The code ends up virtually unreadable. There are far more lines of these types of comments than lines of code.
totalEnergyResidingInAnObjectInJoules = massOfObjectInKilograms*speedOfLightInVacuumInMetersPerSecond^2
oh, and by the way:
gravitationalForceBetweenTwoMassesinNewtons = massOfFirstObjectInKilograms * massOfSecondObjectinKilograms * 6.673e-11 / (distanceBetweenCentersOfTwoObjects ^ 2)
That should put you in your place.
"I don't care about the Constitution!" --Bill O'Reilly, November 17, 2009
Too many folks create "write only" code that isn't worth debugging when it goes wrong and the better way to cure the bug is a rewrite. In those cases the comments are essential to avoid the need for the next guy maintaining the code to have to re-analyse the problem and rewrite.
It gets worse when you add complexity. One thing guaranteed to make code unreadable is when folks create SQL statements with hundreds of predicates and tens of joined tables.
1st simple rule is trust the code not the comments.
2nd simple rule is don't comment the obvious.
Sigs. We don't need no steenking sigs.
Yeah, I re-read it after writing it and hitting submit. It's horrible. My apologies!
~/ssh slashdot.org ssh: connect to host slashdot.org port 22: too many beers
... to the primary objective of getting the program working. Okay, okay, perhaps not... However, trying to construct coherent descriptions and code analysis during a 2am coffee-fuelled session usually results in the babble people complain about. I don't think I've ever opened up source code to see everything perfectly explained, and to be honest, it doesn't need to be.
To me, the most useful comments are usually the ones that give a brief explanation of the objective (of that section of code). Any developer worth his salt can figure out what has been hacked together to achieve said objective. Having text blocks littering the page is just useless unless the original developer was a world class author.
In a southern software company, the original developer (oh yes he was original) decided that it would be a good idea to delete all the comments from the code because he thought you should get inside his code and really understand it and not rely on unhelpful and potentially misleading hints such as comments. I rather suspect that he did it because he was threatened by the chimps they hired as the monstrosity he had spawned began to require more and bodies to maintain - a half a day for the most basic label change which usually went totally wrong. The code was mostly crap with occasional really shitty bits. Certain routines meant to run overnight took several hours. I glimpsed inside this man's head from time to time and was afraid. I'm still baffled how such garbage could have come so far (the company is let's just say not unsuccessful).
All I have to say unless you are absolutely positively sure that you are god's gift to programming and what you consider wonderfully elegant code is universally intelligible - DON'T BE A FUCKING DICK AND COMMENT YOUR WORK.
Suppose you find
Returns: double Foo
*/
public int Foo()
{
return Bar;
}
What does the next developer see in the output of the documentation tool of your choice, and what does the compiler/interpreter see and use?
A doc tool, if any, should damn well parse the relevant parts of the source.
He misses the point. But actually states the point in his objection. Ie That he ignores the comments unless he's stumped. Duhhh... What do you think comments are for? They exist to identify the more non-obvious portions of code. Anybody can see that x = y assigns y to x. This isn't what comments are for. As stated in his #3.
Comments are useful when used correctly. The problem is they usually aren't used correctly. How many times have come back a year later, read a comment, and wondered what the hell you meant by that?
Just like programming, the proper use of comments is an art form. Yes, there are plenty of really stupid comments in code... you don't need line-by-line comments describing the obvious behavior of the code. Well, unless you're writing in APL or some other "write-only" language.. no no-trivial line of APL code is obvious. But proper comments should describe functions, large blocks, and classes... not just what they do or how they work, but what you're thinking and why you did things that might be non-obvious, even when the code looks clear and well written.
There can also be commenting formalisms. I have worked at several companies that used some form of "auto-docs"... basically, when you commented a function, you wrote the documentation for folks calling that function from other modules. I put this feature into the interface description language I wrote as part of an object model for an object-based OS. There were both "formal" and "informal" comments. Informal would be used for very specific little details, formal became part of the hypertext (OS/2 or HTML) generated during every build.
And yeah, in any autodoc-type situation, comment maintenance is part of the job, no less important in the long run than the code itself. In fact, potentially much more important... a bug in your code breaks the system at one point. A bug in your autodoc comments can break the code at any number of points.
This same project also had a mess of pretty cool things. Some weren't useful outside of our object model... the compiler generated a database of pre-compiled object tokens. The release system formally enforced the use of the official release version of the compiler from the server, for a formal release (you could use any compiler you wanted to for testing). This release system also sent release packages out to various other sites in our company, automatically installed, compiled, and merged databases during the "catch" process, etc. You could always build identical code on any PC (OS/2 or Windows NT), with no worries about missing libraries, etc. Not much of it was applicable to code development outside of our specific custom OS, but in the end, it was a very cool system.
-Dave Haynie
Agreed.
When I first learned to write code, it was 1973 and I was 12 years old. Not commenting things worked ok for a few years, when I wrote trivial programs, rarely went back to them, and had a nearly perfect and fairly uncluttered memory.
It wasn't a whole lot different in college. I did learn to write decent comments -- you were sometimes graded on this ability. But again, one project per week in some classes, all your own code, never to be revisited. Comments went one-way... and I'll bet there are coders here who are still at that level.
I got the first hint in my CAD class, which I guess was a Junior or Senior level project engineering course at CMU. In this class, the whole class was engaged in developing the core of a single CAD program. You got everyone else's modules and had to integrate them into the shell of your final project. That was one example of where comments became pretty important.
Here's another one... I started a program sometime in 1986. I wound up working on it, off an on, for the next 12-something years. It became critical to keep things well documented. This was "DiskSalv" for the Amiga computer.
There are other cases.. sometimes, it's that clever hack that took you a long time to figure out. Another Amiga thing was a trick you might find rare these days... you wanted to reset the system without losing your program's control. The problem was simple.. when you hit reset, you normally got a usual 68000 reset vector, but not the one you wanted, necessarily... since upon reset, a ROM magically appeared at the reset vector, and RAM vanished. So I came up with a hack that organized things just so, so I could count on the prefetch buffer allowing me to turn the ROM off and then jump where I wanted. So it was a few lines of code, and all kinds of commenting to explain why this was (far as I know, ever) the only way to do this on any Amiga computer.
Later on, as mentioned in another comment, I worked in places that made comments part of the forrmal documentation... using simple (and not so simple) tools to extract comments from the code, create hypertext links, etc. (on Amiga and later at Scala, Inc.) The ideal place for the documentation on the code is, absolutely, in the code.
-Dave Haynie
One thing I notice (as a former physicist turned developer) is that computer science trained folk are very poor at documenting their *units*. For example, a time duration may be in days, years, milliseconds. Without either naming your variables with a unit name (eg. sleepDurationMillis) or putting in a comment stating the unit of measure at the point of declaration it becomes infuriating and risk-prone using other people's code. Comments are absolutely essential to document the units that variables use. It's also useful having light commentary describing the preconditions of a method and what will cause it to fail - that's one way to get more robust systems - yet few people do it. I only learnt these things after decades of maintaining and extending a lot of other people's code. Therefore I consider the "don't comment" brigade to be rather n00b in their outlook, despite thinking they're l33t.