- 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
// I'm thinking about writing some code. // Maybe a loop. // Yes, a loop is a good idea. // I need some sort of variable to hold the index. // I'll call it "N". // The loop will increment this index variable on each iteration. // OK, I'm going to write the actual code now. // Oh, wait! // There's something wrong here... // The index of the loop is an integer value (it means that there are no decimals). // So I'd rather call it "I", to serve as a reminder of this fact, and facilitate the understanding of the code without the need for a further comment on the integer nature of that variable // Now to the actual code FOR N = 1 TO 10 // TODO : consider trying to code directly // ...
Admin
Nooo...
// Increment i by one, because i needs to be incremeted by one here j=i+1; i=j; // ;-)
Admin
This reads: I can't code.
How about writing your code, if it should do this, like so:
and then writing the functions to make this work?
Why do you insist on writing a clear spec of what your code should look like, then to mangle it into oblivion? Just make readable code already.
Admin
I dunno... maybe I'm just rationalizing because a good portion of my own comments fit this scheme, but I find this style quite useful when I need to make changes.
It's not the "how", it's not the "why", it's the where that matters when I'm in a hurry. I'm able to scan through comments like the example provided and follow the flow to quickly find where to insert/remove functionality, bug-hunt, etc. and I'm not even familiar with the code.
An example that seems pointless in an HTML document:
Someone is eventually going to read that, think "well, no SHIT!", and forward it to be ridiculed here. But faced with a mass of nested HTML tags I can locate things much more quickly by scanning the comments than the id attribs.
Admin
We learn to do that in college! The idea is that teachers are trying to give points from comments in code. But they always says : "Not enough comment" like there should have more comment than code.
Here's some example that occur frequently :
The result is this... we learn that we need to say how and not why since why generally produce fewer comments.
There's two hidden WTF in there. First : some teachers are older than one of my grand-mother. And second : Our computers are so hold that Visual Studio 2008 hang the computer for 5 minutes when you create a new MFC project. (Hint, on my laptop, it take 10 seconds.) Ho yea.... I forgot... it they first session in VS2k8... they where still using VC++ 6 (1998) that can't even process for loops correctly...
Admin
I have to WTF your article here pal. In Steve McConnel's book Code Complete he talks about programming this way, writing out what you are going to do then actually codding once you've got it right. Um, again, the title is "Code Complete". Pick up a copy and read it.
Admin
Personally, I LOVE seeing code like that. It tells you instantly and obviously what went wrong. If the rest of the comments describe pseudo code, and that logic relies on x being incremented, then obviously x-- is an error.
The flipside is that if the code changes, then obviously the comments should change as well to reflect it.
Admin
Ok, so he writes English and then translates, line by line, into the programming language du jour. Code Complete actually recommends this practice as a bug-prevention measure. It separates thinking about the solution from thinking about the fairly arbitrary language in which the solution must be expressed. Not that Code Complete is automatically an authority, but it's likely written to turn majority grunt programmers into something at least a little better.
As to the question of why the code was even written in the first place? Well, are there context clues? Did the author expect that a call graph would be generated? Perhaps this is the result of top-down design with intention-revealing names?
In any event, I'm much happier that the author at least tried to document the code. And this way, a bug is obviously present when code and comment disagree. You may not know which is in error, but you know to go find out.