- 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
great song. Poor WTF. Sure they're obvious, but at least they're present.
Echoing the sentiment about writing comment pseudo-code and then writing code in between. I only do that for complex methods, though. Most of my code runs offa variables
Admin
Couldn't have said it better myself. Obvious commenting may seem like a joke, but remembering what you did x number of months/years/projects/drunken-nights-out ago isn't always easy.
Admin
Thanx, had a good time searching for "don't know why" in Google Code Search!
http://www.google.com/codesearch?q=don%27t+know+why&hl=en&btnG=Search+Code
or
http://www.google.com/codesearch?hl=en&lr=&q=%22don%27t+know+why%22&sbtn=Search
Admin
Amazing. I've always gotten a chuckle or two out of the dumb reader replies, but this thread proves that the readership here has finally hit rock bottom.
Irrelevant comments are worse than no comments. They add visual clutter and make it more difficult for someone to internalize the entire method, class, etc.
Unless you're working on something insanely complicated like a new graphics library or an AI, you should never need to write pseudocode. Write stub methods with descriptive names. Stubs are actual code and don't add redundancy. If you have to write pseudocode, it means that your routine either isn't granular enough or doesn't have a clear purpose. FIX IT.
Finally, if you absolutely must write commented-out pseudocode, remove it after the real code is written. I can't believe how many replies just gloss over this, saying "Oh yeah, I right pseudocode and maybe I leave it in there afterwards, so therefore there's nothing wrong with the snippet here" There is absolutely no excuse for this other than sheer laziness. It's sloppy code. Clean it up, FFS.
Admin
The comments about commenting are an ironic way of pointing out the actual WTF, which is why the author didn't just use
Admin
Aside from the pointlessness and redundancy, another downside to putting in comments like these is that people will read the natural language comments and skip over reading the actual code. Come on, admit it, you did it too. That makes it all the more harmful when the code and comments are out of sync, because you subconsciously assume that you know what is going on from the comments and not actually taking the effort to figure out what the code is actually doing.
Admin
I saw a real WTF with that code - the selection didn't sort on MessageID so the result could end up being unpredictable.
A true seed for weird messages to the user if there had to be a change in the database.
Admin
This is how many institutions try to teach people. If you did something wrong with the code, but say what you meant to do in code, you can still get marks for it. Doesn't matter why you wanted to do it.
Admin
A poor WTF, I have submitted way better stuff than that already.
To the folks that call these comments a WTF: Maybe you should remember that comments should not be about the "why" in most cases, but about the "what", while the code specifies "how" it is done. If your code is so simple that you can tell the "what" and the "why" from the "how", then you may indeed call it self-documenting. If not you should urgently learn these finer conceptual distinctions; it will make you a better programmer. The "what" is the link between specification and code, and it's too often missing. When I see code that doesn't document on this level I sense that the programmer hasn't translated the spec accurately for want of conceptual clarity, and, sadly, too often the quality of the code proves this deficiency.
Conclusion: Tell the maintainer after you what you are doing. The "why" is less important. One day you yourself will be glad about it.
Admin
Back in the day, I was a maintenance programmer on a military mainframe system. The codebase was 20 years old and the coders had been miltary enlisted and officers, most of whom were neither particularly well trained in programming or particularly excited about the thankless job they were "volunteered" into.
The military has, almost above all else, rules. That goes for a military programming shop. Think about it. How many software quality and process czars in a regular development shop can start a paper-trail than can end up with prison time for the stubborn coder?
Anyways, comments were mandatory. The spirit of this mandate was, of course, "explain why you're doing stuff". The implementation of the mandate was always less high-minded.
Numerous places in the codebase, the work of some clearly disgruntled sergeant in ancient times, had comments to the effect of "This comment is here to satisfy Captain <name>'s requirement to have a comment on each line of code."
Admin
Personally, I reckon there's at least one more possibility here. The guy writing the code doesn't know what kind of person's going to be maintaining his code - could be a department where there's a lot of annual university sandwich course students or the like. If that's the case, this could be making sure it's bulletproof in terms of making sure that anyone trying to find a problem knows exactly what each line is trying to do.
True, there ought to be more "we've doing this because there're only 7 internal TPS messages" style comments as well, but I can see the point behind the "now we're going to do this" as well. I'm used to assuming that someone looking at my code may well have less grasp of the language than I do.
Admin
this code goes too far. it's almost "i'm trying to teach the language through comments", and completely ignores why i'm writing the code.
by your statement, I should type in chapters (or at least sections) from a learning C# book for every line i type instead of offering a line or two at the top of an important section.
// retrieving data X from db (it may have changed due to events Y or Z) this says what and why because i'll forget in 2 weeks the how is obvious to anyone who can write code
instead of
// look up array for data x // get data x from the db // load into array for data x
this says exactly how, you can figure out the what if you think for a second, but why am i doing it? what's the point? isn't data x already in the array? i'm sure i had a good reason ... or i was tired ... or i was distracted ... hmmm i'll just remove that code. it was obviously a goof.
Admin
Cut him some slack, he's an injunear not a writer.
Admin
I find lots of code has //TODO: blah in it where it is production ready and tested, but with lots of forward looking comments for future versions.
Just because it's production ready doesn't mean there isn't anything more to do on it, it's just not got the budget, time or client sign off.
I had an manager who insisted i deleted all the TODO comments before i deployed a web app when it was ready. Two months later i was asked where all the new functionality for the next version should be added....
Admin
This sounds like something out of a shop I used to work at. To check a file into source control, you first had to run a home-grown automated code checker. This rigorously enforced a rule governing the proportion of comment lines to source lines. You ended up having to do this simply to fill your quota of comments. (Putting silly comments in pointing out the banality of the system was a good way to get disciplined)
It might have been appropriate in the origins of the company in the sixties, writing really low level stuff, but it was moronic not to change the rules for well-structured object-oriented code.
Admin
"It would be nice to know why the proper range ends in 8, but that's probably documented elsewhere."
If it's not documented here, I doubt it's documented anywhere else, either.
Admin
Personally I comment my code for purely selfish reasons. I don't know why I did things the way I did when I go back over it months later. I've worked on too many things between the time I wrote it and the time I need to modify it to remember much of anything.
So I have both functional and observational comments in my code. To save my own sanity.
My rule of thumb when writing comments - write them so that the person who replaced the person who replaced you can understand what's going on. Essentially write for two generations of programmers out. What seems intuitive takes on a whole new meaning with that idea.
Of course, this didn't stop me from commenting a small procedure completely in haiku one day because I was feeling cheeky.
Admin
Admin
That's when you went into your version control software, rolled back to the baseline with the "TODO" comments and emailed your manager about the change?
Admin
Don't be silly - i laughed in his face and handed my notice in.... :D
Admin
So you weren't using version control, nor did you make a note of what was left to do when you were told to remove the TODO notes from the code?
I'd have resigned at that point too, in order to cover up my incompetence.
Admin
Admin
Ultimately, a comment in code is there for the next few poor souls who come along tasked with maintaining the code. Remember, that could well be you so write things in the comments that will help you if you were coming to the code cold. And it is ever so important to capture the "why" somewhere, especially for long-term support; if you've got a detailed external requirements capture document somewhere, put a link to it in the code that is detailed enough that you'll be able to look up the rationale in seconds. (Yes, I've been involved in maintaining codebases that were over 20 years old. At that age, "why" is truly critical.)
Admin
Addendum (2009-02-11 11:26): EDIT: oh yeah - why I comment my code at work precisely, almost anally: because I have to maintain it. In 3 months, I am not going to remember how it works at all, and a lot of descriptive comments containing both the HOW /and/ the WHY refreshes my mind quite well. The side benefit for my employer is that should I ever leave or be laid off, they aren't left with an unmaintainable mess.
Admin
Ok, the WTF-ness of recent posts is severely lacking. Get with the program, dudes.
But to the topci at hand -- Yes, having answers for 'Why?' would make better comments.
But it looks like these comments come from what is actually not a bad practice -- writing our some very basic logic and/or pseudocode for your work before writing any actual code. This helps you think through the code before you write, often leading to better code quality. And as you fill in actual code, comments are left that look pretty much like what is displayed in this post.
Is it very helpful commentary? Not really. But neither is it a WTF.
Admin
That's jumping to an inaccurate conclusion. I didn't say anything about source control or documentation. That doesn't mean it wasn't there....
I'm sorry to hear about your incompetence.
Admin
Even better yet, 60 references to "thedailywtf"
Admin
I'm deeply worried by the number of posters here who defend this commenting style. Unless you're trying to teach someone language syntax, there's never an excuse for this style of commenting; it simply repeats what's already crystal clear from the code itself if you've written it properly. The code in the article says nothing about the purpose of the code; it's just a stream of consciousness from the developer about the steps they're taking to accomplish this unspecified goal. How is this useful to anyone? If you're coming in to maintain the code later, it doesn't give you any clues about the developer's rationale.
Well-chosen variable and function names and well-thought-out control flow can make comments almost completely unnecessary. Any information that's relevant to the code and can't be made apparent by the code itself still needs to go in a well-written comment that gives the reader all the necessary background they need to fully understand why the code was written as it was; however, any comments that don't fit into this category are clutter and significantly reduce readability.
Maintainability is obviously a concern, but it seems to be often misunderstood what information is most useful to future maintainers (including yourself). I don't need an inline language syntax reference; I need readable, easy-to-understand, straightforward code. The only inline comments that are generally necessary fall into these three categories:
Details about any gotchas that the developers have run into and written code to work around. Example: // TRICKY: Despite what its documentation says, this API returns a 1-based index, so we must decrement its return value by 1 to get the correct index
Hacks (if any) and what the code should more ideally be doing. Example: // HACK: These hardcoded values should live in a configuration file
Things that still need to be done. Example: // TODO: Handle possible error condition if file is not found
API documentation is also important, but is arguably outside the scope of "comments".
Admin
not only that, but since the code only seems to be using 1 of the messages, why does it need to retrieve all 7 from the database? Seems like a waste to me. Probably should be using a stored procedure too instead of building dynamic SQL in the program.
Admin
OK, the real WTF (I hate to use that hackneyed phrase, but feel I must) is that the article emphasizes the comments as the WTF. Every one of us has seen comments like this. Most of us have written comments like this, probably because we were TOLD to by a teacher or supervisor or some such at some point in our education and/or careers. Sure, such comments are useless, but hopefully most of us came to terms with that a long time ago.
What is really f**ed up about this code is what is described underneath it in the article...the fact that it takes an ID, checks if it is in a range, loads all ID's in that range from the database, then uses the original ID to look up the value it wants from an array. Why on earth not replace all of this with one query using the passed in ID?
Admin
I often have to go through and clean up the code (because it doesn't work right) and I just end up completely removing comments like this.
The worst part is that he will sometimes go through my code and put stupid comments like this. I can only assume it's because he can't remember what certain language constructs do and he has to have psuedo-code to understand it.
Often the comments are mostly inaccurate. Usually, it's because he wrote the comment before the code or because he copied and pasted something he wrote earlier. This kind of shit drives me crazy, because I actually take the time to write out nice block comments explaining WHY I am doing what I'm doing, and he just write stupid bullshit.
Examples: // get the selected items var m = gridPanel.getSelections();
// have to convert any new lines into
$comments = str_replace("\n", '\n',$row['comments']);
// clear out the previous tab_items $tab_items = "";
// clean the properties value to send to the function $ids = pgArrayTrim($record['industries']);
// enable the contact and event tabs sub_tabs.getItem('event_tab').setDisabled(false); sub_tabs.getItem('contact_tab').setDisabled(false);
// reload the accounts combo so that the new account // will be in the list dscomboaccounts.load();
WTF
Admin
What he needs is to build a Successor Naming Machine!
Admin
Otherwise known as successors.
Admin
I can't believe none of you have figured out the right answer yet. So let's work it through nice and slow:
So, since the managers can't read the code, the comments are there to provide an English translation. Thus it makes perfect sense to:
Admin
Admin
The real WTF though is that there's probably an insane policy in place that requires a comment on every line, no exceptions, even when it's nonsensical to do so.
Admin
You have some strange managers...
What the heck kind of manager would want to review code if they couldn't read it? What EXACTLY would they be looking for?
Seriously. Please tell me you are trolling.
Admin
System.out.println("Comments? Who uses comments?");
Admin
Yes
Admin
I once worked on a program that was filled with comments like:
I suppose for people who can't remember what "=" and "+" mean, this may have been a useful comment. But as such a person is unlikely to be able to make any sense of the program anyway, the comment was useless. Something more helpful would have been an explanation of what "brdamt" and "crlamt" are. Some sort of amounts, I guess, but what is a "brd" or a "crl"?
I'm amazed at all the posts asserting that useless comments are harmless. As someone earlier replied, they add clutter, which wastes the time of the reader trying to figure out the program. Okay, one useless comment in a 3000 line program would do little harm, but at what point will you concede that useless comments are wasting the maintainer's time? What if there are ten pages of useless comments for every line of code? Surely in that case you would concede that I will waste a lot of time scanning through useless comments trying to find the meaningful comments or the code itself. Well, if there's just one useless comment for each useful comment or line of code, then fully 50% of my reading time is wasted. I've had plenty of tasks where I had to fix a bug or make a minor change that required just a couple of lines of code, but I've had to spend 8 hours searching through the code to find the place to make a change and then 10 minutes actually making the change. If half that reading time was wasted, then my productivity could be practically doubled by eliminating the worthless comments.
Admin
No, I do the same thing - use comments to delineate sections of a large (50-60 line) method. It's handy to be able to call out the large scale logic.
Admin
"Unless you're working on something insanely complicated like a new graphics library or an AI, you should never need to write pseudocode."
You sir are the real idiot.
Admin
This WTF and argument back and forth is ridiculous. You know what, everybody has different coding styles and ways of doing things. Who's to say one person's particular style or preference is better than another. Arguing over personal preferences is pointless. Some people like comments (no matter how superfluous they might seem to others) and some people don't. Its like arguing over what kind of writing utensil that someone uses to take notes and arguing that anyone who uses a pen instead of a pencil is wrong. Anybody who's been coding for any period of time knows there may be many different ways to accomplish the same task. Frankly, if the code does what it's supposed to do in a quality manner, who gives a rats a$$ if ten people have ten different ideas of how much and what kind of comments should be in the code.
Admin
I know what you mean. I often jack up my car to change the oil. I may or may not remove the jack when I'm done.
After all, if it was useful to have it there when I was changing the oil, how can it hurt to leave it there after the oil is changed?
Admin
And don't forget to mention the faulty API version so the comment and workaround don't linger in the code until you're using API version 8.3 when it was really only a problem with version 1.1!
Admin
Why? Why would anyone explicitly create a NullReferenceException()? Any code that dereferences a null pointer has a programmer's error. Fix the code!
Admin
Admin
This isn't about personal preference, it's about software quality and ease of maintenance. Those things are objectively measurable (assuming a reasonable working definition of "quality"). Trivializing it as if it's personal preference only buries the problem deeper and perpetuates an attitude of indifference toward something with real detrimental impact. Yes, in the strictest of terms, comments have no direct effect on the quality of software after it's compiled, but their indirect effect is only one step removed from it. The philosophy of "if it does what it's supposed to when it compiles, it's good code" really isn't applicable in many situations these days.
Admin
Because they never learned why they should comment their code.
The Professor said you must comment your code so they started doing stuff like this:
i++; // increment loop variable by one
As I've made my transistion from the classroom to a real world programmer I think most problems can stem from how we were taught.
In the classroom I receive an assignment, I code it and turn it in. The only person who will ever read the source, other than me, is the Professor. The only one who will ever have to maintain it is me(and usually these are 'one-offs' which even I will never look at again).
How do you teach CS concepts like that? If no one is ever going to have to maintain it why document it? If no one besides you is ever go to touch it why worry about encapsulation, source control, etc?
What they should do is this: Have everyone code a project and turn it in. Then pass the projects back out to other people. Now everyone has to add a feature to someone else's project. Suddenly all these CS concepts become clear. You don't really comprehend why you need to document your code (and how to do it well) until you've struggled to understand somebody elses undocumented code. You don't understand the principle of orthogonality until you've had to work with someones code that has lots of unexpected dependencies etc...
Admin
I have to agree that I like ending functions/subroutines etc with 'end;// function functionName' If you have either long functions or subroutines that cross more than one screen, it's easy to scroll past the end/start and jump into the next function. Sometimes it's easy to tell you did it, sometimes it's not, but putting the function name at the end of the function certainly makes it clear when you've managed to skip from one to the other.
On the other hand, I've seen too many bits of scripting where the psuedo-code and the code had very little in common.