Page 1 of 1

Those of you who are programmers should find this amusing.

PostPosted:Thu Jun 09, 2005 10:22 pm
by Manshoon
<a href="http://www.thedailywtf.com">The Daily WTF</a>

PostPosted:Fri Jun 10, 2005 3:02 am
by SineSwiper
Nice stuff here.

PostPosted:Fri Jun 10, 2005 12:17 pm
by Nev
That first example looks like a good one for my list of "code designed to drive the sane man mad". My first realization that there really are programmers who can't code their way out of a paper bag was at my first real programming job, when I realized our lead programmer was writing things that looked like this:

int DoSomething(int a)
{
int b,c;

b = a;
c = b;

return DoSomethingElse(c);
}

That was the precise moment I realized our current project was doomed (as it indeed turned out to be)...I don't write perfect code all the time, but there is indeed some REALLY BAD code out there, even in production code.

PostPosted:Fri Jun 10, 2005 2:30 pm
by Kupek
This isn't wrong in any way, but it'll make you do a double take.

<a href="http://www.lysator.liu.se/c/duffs-device.html">Duff's Device:</a>
Code: Select all
send(register* to, register* from, register count)
{
	register 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);
	}
}

PostPosted:Fri Jun 10, 2005 3:20 pm
by Lox
That is some nice stuff. :) I'll go through it all later.

I remember working with this in-house scripting tool to create test data and one of the guys in my group had some code that looked something like this:

if a = 2
{
if a = 3
{
Print("WOOHOO!")
}
DoOtherStuff()
}

I always thought that was funny. :)

PostPosted:Fri Jun 10, 2005 3:24 pm
by Nev
Kupek wrote:This isn't wrong in any way, but it'll make you do a double take.

<a href="http://www.lysator.liu.se/c/duffs-device.html">Duff's Device:</a>
Code: Select all
send(register* to, register* from, register count)
{
	register 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);
	}
}
How does the brace after the 'do' not invalidate the rest of the case tokens? I thought a switch statement would go out of scope if you start a new block.

PostPosted:Fri Jun 10, 2005 3:39 pm
by Kupek
Mental wrote: How does the brace after the 'do' not invalidate the rest of the case tokens? I thought a switch statement would go out of scope if you start a new block.
Nope. You can use braces inside the cases for a switch statement (in C/C++, at least, I don't know for other languages that have similar syntax). You can group multiple statements associated with one case using braces.

PostPosted:Fri Jun 10, 2005 3:52 pm
by Nev
I know <i>that</i>, but I was under the impression that this is right:
Code: Select all
switch(i)
{
   case 0:
   {
       //do stuff
   }
   break;
default:
   break;
}
But this is wrong:
Code: Select all
switch(i)
{
   case 0:
   {
       case 1:      // I would think this is illegal because the sub-block
                       // takes the switch out of scope, making it a case without
                       // a switch - no?
       {
            //do stuff
       }
       break;

       //do stuff
   }
   break;
default:
   break;
}

PostPosted:Fri Jun 10, 2005 4:14 pm
by Kupek
Nope, it's legal. Once you're in the scope of the switch, you can't leave it until the final brace. Consider the following program.
Code: Select all
#include <iostream>
using namespace std;

int main()
{
        const int NUM = ?; // change to 0 or 1

        switch (NUM)
        {
                case 0:
                {
                                cout << "inside 0" << endl;
                                case 1:
                                {
                                        cout << "inside 1" << endl;
                                }
                }
        }

        return 0;
}
If you define <tt>NUM</tt> to be 0, then it prints
<blockquote>inside 0
inside 1</blockquote>If you define it to be 1, then it prints
<blockquote>inside 1</blockquote>Duff's Device, however, is the only piece of code I've encountered that exploits that fact.

PostPosted:Fri Jun 10, 2005 6:07 pm
by Nev
That's interesting. I may have to remember that as a trick to show high school students if I ever do well enough to realize a dream I have of going back and being a guest lecturer at my high school one day...ah, dreams...

PostPosted:Fri Jun 10, 2005 9:55 pm
by Ishamael
Programmers trying to get cute. Though compared to some Perl software I've seen, these guys are rookies. :)

PostPosted:Fri Jun 10, 2005 10:00 pm
by Ishamael
Mental wrote:
Kupek wrote:This isn't wrong in any way, but it'll make you do a double take.

<a href="http://www.lysator.liu.se/c/duffs-device.html">Duff's Device:</a>
Code: Select all
send(register* to, register* from, register count)
{
	register 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);
	}
}
How does the brace after the 'do' not invalidate the rest of the case tokens? I thought a switch statement would go out of scope if you start a new block.
Yeesh. Looks like programmer trying to turn C into Lisp or something.

On the one hand, I'm reminded of why I hate C. But on the otherhand, it's handy to be able to do stuff like that at times. :)

PostPosted:Fri Jun 10, 2005 11:11 pm
by Nev
I find it really handy that C (and C++ even) are so low-level - oddly I find in some ways it actually makes it easier to work with than, say, Java.

PostPosted:Sat Jun 11, 2005 11:17 am
by SineSwiper
There are plenty of wrong ways to write Perl. Frankly, I'll scrap slight optimizations in favor of readability any day. I loathe using $_ except in array sort/grep/map routines, because it makes code increasingly errr, assembly like. (You know, putting data into register A, and then running a bunch of commands that use register A for its input. Part of the way throughout the code, you're confused as to what exactly the commands are referencing or what the data is even supposed to be because the variable isn't named something self-explainatory.)

PostPosted:Sat Jun 11, 2005 12:59 pm
by Kupek
Ishamael wrote:Yeesh. Looks like programmer trying to turn C into Lisp or something.
Lisp? That seems like a very un-functional technique. But anyway.

C and C++ can indeed be low level. But in C++, through the STL and the abstraction mechanisms provided by the language, it need not be.
SineSwiper wrote:There are plenty of wrong ways to write Perl. Frankly, I'll scrap slight optimizations in favor of readability any day.
That's the best way to approach it in any language. Generally, a good optimizing compiler is going to factor out little things like that. Optimizing code isn't worth your time unless you know two things: you've timed it and it's not fast enough for your needs, and you've profiled it, so you know where the bottlenecks are.

PostPosted:Sat Jun 11, 2005 8:01 pm
by Nev
SineSwiper wrote:There are plenty of wrong ways to write Perl. Frankly, I'll scrap slight optimizations in favor of readability any day. I loathe using $_ except in array sort/grep/map routines, because it makes code increasingly errr, assembly like. (You know, putting data into register A, and then running a bunch of commands that use register A for its input. Part of the way throughout the code, you're confused as to what exactly the commands are referencing or what the data is even supposed to be because the variable isn't named something self-explainatory.)
I'm reminded of a quote from a more senior programmer at my first programming job - "You can program badly in any language." Seems kind of harsh, but it really is true.

PostPosted:Sun Jun 12, 2005 1:53 am
by SineSwiper
Mental wrote:I'm reminded of a quote from a more senior programmer at my first programming job - "You can program badly in any language." Seems kind of harsh, but it really is true.
Yeah, but it seems like that is doubly true with Perl. Despite the fact that the language was invented by a linquist and is capable of producing very elegant code, there are many different way to implement the same function, so there are also that many ways to fuck up the code beyond belief. Couple that with the fact that most Perl programmers do seem to use $_ a lot, it gets annoying to read a lot of Perl code, other than your own.

I won't even get into Perl modules. Everybody seems to code those like they were coding in C++.

PostPosted:Sat Jul 09, 2005 2:10 am
by Manshoon
Bwahahaha!

<img src="http://thedailywtf.com/forums/37986/PostAttachment.aspx">

The story behind this is freaking hilarious, definetly worth checking out.

PostPosted:Sun Jul 10, 2005 6:04 pm
by SineSwiper
Reminds me of the wiremess image that The Register was showcasing a few years back. I need to hunt down that article.

Image

This image, but I can't find the story that goes with it.