- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
The presence of "not" in a variable name (e.g. isNotHalted) is probably very close to a sufficient measure for a negatively variable name, but I don't think it's necessary either. (To be honest I don't even really like if(!container.empty()) and would prefer if(container.hasStuff()) if there were a better name for it.)
Admin
Admin
Admin
HOURAY !!!!!!!
Admin
Ah yes, Turbo Pascal. I had a program which would only work if there was a comment in a particular place. Any comment. Which is why the program ended up containing a comment saying {If you remove this comment, the program will stop working}.
Happy days.
Admin
For some reason this reminds me of the fairly common (and equally effective) concept of praying among human beings.
Admin
I'm happy equally happy with
while(continue) or while(!finished)
HORSES FOR COURSES PEEPS!!!!
Admin
Better?
Admin
I tend to agree. Far to many people comment in a cargo cult fashion. No comments are better than useless, misleading, redundant or excessive comments. Comments for the sake of comments never results in good things.
Real programmers can read code well enough to not need Rosetta comments everywhere as long as function names and variable names are reasonably accurate and the code isn't too disorganised. The most important thing for a coder to be able to do is to be able to write code, good code. The ability to write comments (at all) is secondary to this.
Comments aren't totally needless though. You obviously want them for particularly complicated or obscure pieces of code, API documentation generation (as soon as your code base reaches a certain size you will need this) and for preprocessor directives.
I prefer to hardly comment code that is being created at all in many cases until later and if at all (unfortunately time is not always permitting). The later you leave it to the less over head as once you comment a method you'll have to change the comments if you change certain aspects of a method. Whether or not this is applicable depends on your development flow.
Admin
lol :)
Partially right. The "it's" that you insert is implied - this is common in English, and is grammatically correct. I did, however, forget to include "and" before "helps"
:)
Admin
I agree. if(!(isNotRunning)) or if(!(a&&b||c&&d||a&&c)) often forces the reader to do a lot more work than the inverse.
Admin
If you have to do something screwy because of a bug in something else, use comments to document what the bug is and what version the bug was found in.
I had some third-party source code which triggered a bug in a cross-compiler I was using. (The compiler generated an invalid assembly language statement!) It turned out that simply declaring the particular variable as "volatile" was enough to fix it, so I put "#if 1 / volatile / #endif" in front of the variable's declaration, with comments about which version of the compiler it happened in. (It was in fact fixed in a later version of the compiler.)
Admin
Well, really now:
Probability that another developer reads the code: close to 1
Probability that another developer tries to understand the code before making changes: 0.5
Admin
And very, very often, you code is not as clear as you think.
Yes, any competent programmer who reads a line of code that says, for example, "amount=price*quantity" should be able to figure out that you're multiplying the price by the quantity. If he can't figure that out, this person should not be a programmer. And MAYBE the purpose of that line is obvious in context and doesn't need a comment. But a category of very useful comments are the ones that say "Must do this before calculating sales tax" or "price is from the line_item record, not the product_master". There are all sorts of minor subtleties that may not be obvious to someone reading this six months later.
And where comments are really useful is to tell me the overal purpose of this block of code. Yes, I can hack through and see that you're adding 1 to x here and dividing by two over there. But what are you trying to accomplish? A simple comment like "Find all employees who retired in the past year and update their benefits" can save someone a lot of fumbling.
Admin
What usually happens is that 6 months after writing a code unit (class, method, whatever), you have to do some maintenance of it, so spend several time-units working out what it did. That's when you comment what it does, so you don't have to figure it out again. You will also find those comments better than the ones you would have written when you initially wrote the code.
Admin
Should also have explained "This oddity is caused by a bug in dtksh which causes it to crash if there are no print statements in it to capture data."
Admin
BTW, my absolute favorite comments are those that state the obvious. Like not long ago I came across this line:
Well, wow, thanks for that comment. Yeah, I forgot what "+" means in Java.
It would have been nicer still if you'd told me what "x" and "y" are, though. If you're going to give your variables meaningless names, AND you're going to write comments, maybe you could use the comments to tell me what the variables are instead of giving me a tutorial on Java.
Admin
Excellent point. I recently wrote some code that seemed like the simple and obvious way to get the job done, but it didn't work because of some subtleties. So I figured out how to do it in a more complex way that did work. But it occurred to me that someone coming along later might say, "This is stupid, why don't you just ...", and then delete the working code, put in the "obvious" code, and then find out why it doesn't work and have to put it all back. So I left in the non-working code, commented it out, and put in a comment that said "This doesn't work" and a sentence about why not.
Admin
frankly i envy the people screaming "ALL CODE IS INHERENTLY SELF EXPLANATORY, OR IT SUCKS" because these people have obviously never had to write in WebFOCUS (hope you like GOTOs and 6-character-max variable names), or get some ajaxy thing working in a half-dozen ancient browsers because of reasons beyond the developer's control. we don't all work in paradise.
Admin
to elaborate: most of my comments are explaining how the following apparently-nonsensical block of code skirts around a stupid bug in the product.
Admin
Never heard of automated testing?
Admin
[I'm not saying that Jay's approach is the only way to do things. You could write up a quick summary of the broken approach instead of leaving the code, and put in a pointer to the test. Or if you co-locate the code of the thing under test with the actual test you could reduce things further, but I don't do that and I get the impression that my way is more common.]
Admin
I write the comments first. It helps me map out what I want to do, and then I write the code to fit, adjusting the comments as necessary.
Admin
Admin
The semi-famous Unix v6 comment "You are not expected to understand this" which comes after some explanation is a good example. Dennis Ritchie ( http://cm.bell-labs.com/who/dmr/odd.html ) points out that that was an example where they didn't understand it, and had to rewrite the code when porting to be correct.
Admin
The better solution is to switch to a bug tracking tool that can integrate with your VCS. How often do you need to know "which bug did this code fix?" Versus "what code fixed this bug?"
Admin
// // Dear maintainer: // // Once you are done trying to 'optimize' this routine, // and have realized what a terrible mistake that was, // please increment the following counter as a warning // to the next guy: // // total_hours_wasted_here = 42 //
Admin
I have to admit that I have actually done this once years ago in an ancient VB6 application. If I remember correctly, it was not even an error handler, but a select case statement with a seemingly impossible enum value. The brilliant™ piece of code was in production release for years, until one customer sent us a screenshot of a rather embarrassing message box.
Admin
Unfortunatly I believe noone is capable of writing clear and concise code all the time. Often by external factors, most likely time-constraint. Further, it can be a huge time saver when trying to figure out a new framework or class or ... If theres some comments at the class definition you can instantly know what such an object is used for, when and why and if this class is of any use for you before reading possibly 1000+lines of code that may not even be in a language your familiar with.
Admin
Wronged generally has it the wrong way around, the developers need to know how the code relates to the real world and any such comments should be targeted at them. However in this case you need to use the language of the domain the program relates to. This is necessary if you are to have an expert in the domain ensure your understanding is correct amongst other things. Ultimately you are creating a map.
Admin
What seems to happen more often than not is that developers are lazy (which BTW is never a good attribute for a programmer), and instead of taking the time to move this logic into separate modules, they just copy/paste it around and make subtle changes where necessary, and the code-base grows 3 or 10 or 100 times bigger than it needs to be. Most of the code is inlined at outer-layers and functions are hundreds of lines long.
No amount of commenting will fix this, even if you think it will. Those same lazy developers that copy-pasta are going to also neglect those comments when they change and re-arrange things.
Admin
Application that I stepped in to develop had some twenty forms completed, each with a help button. Every one of callback methods for the button had comment:
I felt very welcome there.Admin
I write comments for myself, NOT for anyone else. My code is self-commenting. Even so there are times that even the best code gets too complicated (read advanced techniques). That's when I add a comment. So that I, Myself, and me won't have to spend a half-hour deciphering it again on my next visit.
There's one other type of comment I leave in languages that support it. /* Yes, I used a goddamn GOTO here. It's simpler than the mess I'd have to make by NOT using it. If you don't like it, either FO, or rewrite it and spend 4 hours every time it needs a future change. */
Admin
Sorry mate, if your code block spans over such huge blocks you are getting confused, you really need to refactor. As code evolves, comments become stale. Do you really need the overhead of having to maintain code and comments?
Consider this:
As you said yourselves: "stupid and annoying". During code maintenance, I would remove comments like this immediately. Pointless cruft cluttering the picture - causing exactly the difficulty in "working out the flow" - quoting you again.
Clearly, if someone cannot instantly parse "int i;", for what it is, then reading this code is not really theirs, is it?
Admin
while(!finished) is pretty clear.
while(!notCompleted) will make you stabby after a couple of times.
Admin
Seen this a few days ago: OpenFileDialog d = new OpenFileDialog(); d.Filter = "csv files (.csv)|.csv"; //set filter to .txt
So... either the code is lying, or the comment!
Admin
stromectol posologie vidal order stromectol over the counter
Admin
dividere pillola cialis cialis capsules
Admin
stromectol 3 mg tabs stromectol 3 mg uses