- 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
In my first computer job, I was tasked to modify about six pages of assembly language code-- a device driver for a speech synthesizer.
The code was breezy and uncluttered, by comments that is.
There was one and only one comment in the six pages of code, on a line that sent a command to a custom clock device, something like:
.. that's where I first started getting a glimmer that code in the real world was somewhat unlike code we wrote in class.
Admin
Re: comments like
It's useful if the guy who made the WTF you just fixed still works with you and on the code. Specially if that guy is your boss.
Admin
From the usual source, Wikipedia, we get:
Admin
As an old fart, I recall the source of James Gosling's implementation of emacs, sometimes called gosmacs. In the beginning of display.c (did, um, display management / optimization) there was a big ASCII graphic skull and crossbones. The text was something along the lines of "no matter how well you may think you understand this code, you don't, so don't mess with it."
Admin
If you're used to it, it not much different. For a chuckle, enter this:
It will immediately drop the & suffix because it thinks "ah, number long enough, long suffix not necessary." Very helpful, considering that if you change anything trivial (like spacing) on the line later it will change to: An integer constant! Although the (decimal) printed value is the same, there are a number of situations where it behaves differently :-) Morale: if this happens when the type matters, use an explicit type. Perhaps declare a constant or var, or use a conversion function, depending on your needs.Admin
Admin
Yeah, source control is great. I mean, why add a one-line comment when you could just make people trawl the repository logs, possibly going back years and years, to find out who added or changed a chunk of code?
Repository spelunking is what you do to find out who the shlemiel who didn't follow the coding standard and document their changes was.
Admin
By the way, the above example is adapted from "How To Write Unmaintanable Code" (http://thc.org/root/phun/unmaintain.html). Other examples include:
Struct/union and typedef struct/union are different name spaces in C (not in C++). Use the same name in both name spaces for structures or unions. Make them, if possible, nearly compatible.
Hide macro definitions in amongst rubbish comments. The programmer will get bored and not finish reading the comments thus never discover the macro. Ensure that the macro replaces what looks like a perfectly legitimate assignment with some bizarre operation, a simple example:
Instead of using
break up "xy_z" onto two lines:
That way a global search for xy_z will come up with nothing for that file. To the C preprocessor, the "" at the end of the line means glue this line to the next one.
Maintenance programmers, in order to see if they'll be any cascading effects to a change they make, do a global search for the variables named. This can be defeated by this simple expedient of having synonyms, such as
These defs should be scattered through different include-files. They are especially effective if the include-files are located in different directories. The other technique is to reuse a name in every scope. The compiler can tell them apart, but a simple minded text searcher cannot. Unfortunately SCIDs in the coming decade will make this simple technique impossible. since the editor understands the scope rules just as well as the compiler.
Overload the "new" operator - much more dangerous than overloading the +-/*. This can cause total havoc if overloaded to do something different from it's original function (but vital to the object's function so it's very difficult to change). This should ensure users trying to create a dynamic instance get really stumped. You can combine this with the case sensitivity trickalso have a member function, and variable called "New".
Go wild with encapsulation and oo. For example:
That one probably did not even seem funny. Don't worry. It will some day.
(etc)
Admin
Comments with the hex value can be useful. I've done it a couple of times when the constants represented external values. That way you can search in either hex or decimal and find it.
Admin
The real WTF (sorry!) is that he's not using the standard ASCII control characters.
Should be (forgive any syntax errors; I'm not a VB programmer):
Captcha: tesla (isn't that an 80s Christian hair band?)
WTF is with the extra blank lines?
Admin
The comments link is broken on all articles. I read them in Google reader, then have to click on the article title and then the comments link. However, this link brings me to the main articles page as it points to: http://worsethanfailure.com/Comments/Comment_in_Earnest.aspx
but on the articles page, the link is to: http://worsethanfailure.com/Comments/Comment-in-Earnest.aspx
Note the difference - one has underscores, one has hyphens. Fix the links!
Admin
Some other favorites:
Jude the Obscure
Always look for the most obscure way to do common tasks. For example, instead of using arrays to convert an integer to the corresponding string, use code like this:
Foolish Consistency Is the Hobgoblin of Little Minds
When you need a character constant, use many different formats ' ', 32, 0x20, 040. Make liberal use of the fact that 10 and 010 are not the same number in C or Java.
Always use semicolons whenever they are syntactically allowed. For example:
Smuggle octal literals into a list of decimal numbers like this:
Follow the language lawyer discussions in the newsgroups about what various bits of tricky code should do e.g. a=a++; or f(a++,a++); then sprinkle your code liberally with the examples. In C, the effects of pre/post decrement code such as
are not defined by the language spec. Every compiler is free to evaluate in a different order. This makes them doubly deadly. Similarly, take advantage of the complex tokenising rules of C and Java by removing all spaces.
Admin
http://www.de.ioccc.org/2000/primenum.c
That is all.
Admin
this is not half as efficient as:
and yes, i'd accept this kind of code, if there really was no loop unrolling in the compiler.
Admin
Oh, and my absolute favorite:
Admin
Any decent source control system will have an easy to use 'blame' function. I suggest reading up on what it does.
Admin
I found once a comment in the middle of some php code that went something like:
//This is a bad way to do this, but it works
The code that followed was a textbook example of the for-case paradigm
Admin
I've seen people lose lawsuits because they added their initials and date to a chunk of code....
Admin
that's absolutely amazing
Admin
Shouldn't that be,
Admin
That's what the blame and log features of SCM tools are for. Comments with programmer name/change date are noise (very expensive noise, once maintainance developer thinking time is accounted for) and I generally delete them on sight if I'm already editing the source file for other reasons.
Seriously, I know that even if I thought it was a good idea and intended to leave such a trail, I'd still get it wrong eventually. I certainly wouldn't trust anyone else to get it right, especially if they're sloppy enough at whatever they're doing that I have to start asking the SCM for line-by-line changelogs.
Admin
Admin
I'd say that the comments within the function definitions are gratuitous. I see a bigger WTF in not return the pop'ed value.
OTOH, the comments in the definitions are actually a very useful courtesy. Certainly the assignments could have been to hex values with comments offering the decimal equivalents, and provided the same courtesy, but that would have been a bit more verbose to achieve the same clarity.
Whether these were necessary or not really depends on the wider context in which the code is used. If there is absolutely no possibility that the code would ever have to interact with both decimal and hex values, then there is no point in the comments. If there is any point at which the context switches from hex to decimal, then the comments have saved some grunt work for the maintainer.
For what it's worth, I do very little commenting in my own code. I try to choose identifiers that are self-commenting. I also think that comments that actually provide useful information are just fine.
Admin
sigh Amatuers. To optimize properly one must use a profiler, so one can see the results of ones changes. After many hours of careful timing and tweaking, I believe I have found the ultimate implementation. I have helpfully commented each step in my optimization so you can grasp it more clearly.
Addendum (2007-04-18 14:03): Curses, I forgot to use y to fill the array, it's always the little things that get you >_<
Admin
I had the joy of working on some code, where all the variables were animals. Toad, Frog, Emu, Dog, Cat, Bird, Rabbit, Monkey, etc...
/Captcha is 'craaazy', which is what the code made me!
Admin
I think I detect some implicit sarcasm tags here, but I wouldn't scoff too quickly. Bear in mind that most full-featured programming languages implement essentially the same set of logical operations. Translating skills from one language context to another should be a rather straightforward task.
My own experience has been that getting the first code snippet to run is often the hardest task. You encounter all of the esoterica of the particular compiler/interpreter/invocation procedure in getting started. Once I have successfully run "Hello, World" in any language, the rest is gravy.
Admin
Admin
Well yeah, if you happen to be up to your neck in hex dumps debugging network packets. But what if you have someone who doesn't know what STX means or what it is that SETACTIVE is activating? What if you want to know the format for the SETACTIVE command?
It's nice having the hex values, but a little explanation would be nicer imho
Admin
Awesome, that code runs in 0ns on my machine, perfectly optimized!
Admin
Regarding push/pop vs enqueue/dequeue:
The 2nd WTF example actually follows the C++ standard. It looks like it's re-implementing (why? why?) the std::queue adapter, which you can read about here: http://www.sgi.com/tech/stl/queue.html
STL also has a stack adapter here: http://www.sgi.com/tech/stl/stack.html
Note that the APIs for both adapters are similar. They have identical in terms of push & pop to insert/delete elements. To read data, queue gives the front & back methods while stack only gives the top method.
Some might think that the naming indicates that the coder doesn't know WTF the difference is between a FIFO/LILO and FILO/LIFO, but for those of us familiar with STL, it's obvious to us that this is no departure from the standard.
If anything, I'm just puzzled that the original WTF code exists and why std::queue just can't be used directly, but I'm not privy to the entire class so maybe there was a reason. The fact that they ordered their container back-to-front instead of front-to-back doesn't bug me in the slightest; makes the comment a little weird and I would have been wordier about it, but doesn't point to an actual code problem. Users of the code, presuming they understood STL and it was defined as a Queue-type class, will have no problem using this class & getting expected results.
If you find the push/pop names confusing on a queue, the deficiency is either with you or with the C++ standard (depending on your disposition), but not with the WTF coder.
Admin
What's wrong with supplying a comment that notes what a value is in hex/decimal? If the value was in hex, I'd like to see a decimal translation... at least for constants. Both can be useful to know.
Anyway, I don't think it is nearly as bad as "// increments n"
-matthew
Admin
I vote for:
Self-Commenting Code!!!!
Admin
tags inside
Admin
Egads!! If the author disliked C this strongly s/he should have chosen another language. Looks like a tantrum to me.
Admin
I think the irony of that is that we have this:
BeginProgram OpenBrace
followed by this:
EndOfProgram CloseBrace
From the #defines, it makes sense, but on the face of it, it looks like a nesting error.
Admin
"it's just a matter of whom"
Actually, it's a matter of who
Admin
The comments are technically correct. This guy implemented a data strucutre. If you want to add to the front of the data structure you call push, which will push something onto the beginning. If you want to remove something, you call pop, which removes it from the end.
Internally he decided to push back, and pop front. As the person using this code, you don't need to know the implementation details. You don't need to know it was implemented ass backwards. (I mean its not like there is no push_front, pop_back)
Admin
Admin
Admin
In ancient days (1973) I worked on a large project in PDP-11 assembly language. Most of the subroutines were commented like this (all comments shown):
aname: mov fp,-(sp) ; save ... (maybe 60 uncommented lines) mov (sp)+,fp ; restore rts pc
If a subroutine had an error, it was usually easier to rewrite it entirely than to figure out:
Programs were a little simpler then. But I no longer have to load a paper-tape boot loader using front-panel toggle switches.
Admin
Using reserved identifiers... nice
Admin
So what ?!?
In the first set of comments somebody noted down hex notation for his constants in comments to save the continous recalculation. True, I haven't checked if the dec and hex value pairs correspond correctly with each other but that, I think, is not the point .....
The second set of comments is of course wrong: if you implement a stack with such simple procedures then you do not need comments at all. Single line functions do not need comments and any coder who does not know how a stack works is pathetic indeed ....
All in all, the OP is not very WTFy at all ... more like a digestive cookie compared to the usual complete cake Alex usually serves up.
Admin
In VIM you do ":syntax on".
Admin
Wow. The WTF is in the standard. I haven't used C++ for over 10 years, and the STL wasn't completed at that point. We had to make those data structures by hand...
Admin
I'd suggest chosing a source control tool that is able to give you that information automatically. IMHO all changes by grep means all changes the programmer remembered to comment.
/axl
Admin
Sorry, couldn't resist being annoying, it's raining.
/axl
Admin
I vote for VB syntax in C++:
Admin
Aargh! What is with these idiots who put their name in comments? Worst practice ever. CVS and build revision logs will take care of that, I want to be able to read the code and understand it, not spend all my time reading who changed what.
Even worse: // fixed bug 5456789
What the fuck was the fix, why is it doing that, explain what was non-obvious and led to the bug! Rages impotently
Admin
I used to think that was useless, until I worked at a place that changed version-control systems every so often (and lost all the version history each time) so that those initials became the only way to identify who made a change.
Admin