The Other Worlds Shrine

Your place for discussion about RPGs, gaming, music, movies, anime, computers, sports, and any other stuff we care to talk about... 

  • Those of you who are programmers should find this amusing.

  • Somehow, we still tolerate each other. Eventually this will be the only forum left.
Somehow, we still tolerate each other. Eventually this will be the only forum left.

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

 #88539  by Nev
 Fri Jun 10, 2005 12:17 pm
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.

 #88551  by Kupek
 Fri Jun 10, 2005 2:30 pm
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);
	}
}

 #88556  by Lox
 Fri Jun 10, 2005 3:20 pm
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. :)

 #88558  by Nev
 Fri Jun 10, 2005 3:24 pm
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.

 #88561  by Kupek
 Fri Jun 10, 2005 3:39 pm
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.

 #88562  by Nev
 Fri Jun 10, 2005 3:52 pm
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;
}

 #88565  by Kupek
 Fri Jun 10, 2005 4:14 pm
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.

 #88573  by Nev
 Fri Jun 10, 2005 6:07 pm
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...

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

 #88584  by Ishamael
 Fri Jun 10, 2005 10:00 pm
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. :)

 #88588  by Nev
 Fri Jun 10, 2005 11:11 pm
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.

 #88599  by SineSwiper
 Sat Jun 11, 2005 11:17 am
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.)

 #88609  by Kupek
 Sat Jun 11, 2005 12:59 pm
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.

 #88617  by Nev
 Sat Jun 11, 2005 8:01 pm
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.

 #88630  by SineSwiper
 Sun Jun 12, 2005 1:53 am
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++.

 #89784  by SineSwiper
 Sun Jul 10, 2005 6:04 pm
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.