Those of you who are programmers should find this amusing.
PostPosted:Thu Jun 09, 2005 10:22 pm
<a href="http://www.thedailywtf.com">The Daily WTF</a>
Your place for discussion about RPGs, gaming, music, movies, anime, computers, sports, and any other stuff we care to talk about...
https://tows.cc/phpBB2/
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.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 allsend(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); } }
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.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.
switch(i)
{
case 0:
{
//do stuff
}
break;
default:
break;
}
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;
}
#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;
}
Yeesh. Looks like programmer trying to turn C into Lisp or something.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.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 allsend(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); } }
Lisp? That seems like a very un-functional technique. But anyway.Ishamael wrote:Yeesh. Looks like programmer trying to turn C into Lisp or something.
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.SineSwiper wrote:There are plenty of wrong ways to write Perl. Frankly, I'll scrap slight optimizations in favor of readability any day.
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.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.)
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.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.