Comment On The Case for Switch

Little more than a structured goto with a nicer getout, every C programmer knows (and many love) the switch statement. Even if you don't love it, there are obvious uses that produce cleaner code than strings of else-ifs and help prevent many simple kinds of errors. [expand full text]
« PrevPage 1 | Page 2Next »

Re: The Case for Switch

2007-04-26 09:21 • by mav (unregistered)
Haha, terrible, just terrible code. But funny.

Re: The Case for Switch

2007-04-26 09:32 • by bonzombiekitty
133734 in reply to 133731
edit. oops. misread the first one.

Re: The Case for Switch

2007-04-26 09:32 • by Ken (unregistered)
Hmm...

If it's a comma, and remains a comma, replace it with a period.

If it's still a comma, and it changes to a quote, replace it with an apostrophe.

Re: The Case for Switch

2007-04-26 09:32 • by Joel (unregistered)
"how many times do you have to check a condition?"

Well we know how many times he thinks he needs to check it.

Re: The Case for Switch

2007-04-26 09:34 • by bonzombiekitty
The first one must be the in the future when there's quantum computing, where a character can be an comma and a quotation mark at the same time.

Re: The Case for Switch

2007-04-26 09:34 • by anonymous (unregistered)
"there are obvious uses that produce cleaner code than strings of else-ifs and help prevent many simple kinds of errors".

And introduce another ones. ;-)

Re: The Case for Switch

2007-04-26 09:39 • by exaxe (unregistered)
At least the second snip has a call to the assEvH function.
That has a chance of doing something useful.

I wonder if there is an entire ass library.

Re: The Case for Switch

2007-04-26 09:43 • by mkb
zWindow * child;


ZWINDOW STARCHILD

I especially like the switch that does nothing.

Re: The Case for Switch

2007-04-26 09:49 • by wiregoat (unregistered)
133742 in reply to 133739
I have one. Not sure what it has to do with programming though.

captcha doom. I am in a backwards mood today.

Re: The Case for Switch

2007-04-26 09:52 • by Chris Walton (unregistered)
Scare away newbies with a one-liner equivalent

void ReplaceCommas (char* p) {

for(; *p; p++) *p = *p == ',' ? '.' : *p;
}



CAPTCHA: muhahaha (how fitting!)

Re: The Case for Switch

2007-04-26 09:54 • by H|B
Mwuahuhauhauhauha

The second one is so ugly, it reminds me of: syntatic sugar causes cancer of the semicolon.

Re: The Case for Switch

2007-04-26 09:58 • by dkf (unregistered)
if (buffer[i]==',')

{
if (buffer[i]==',') buffer[i]='.';
}
This could be OK if this was C++ and suitably defined operators were available which transmogrified the state in places that the naive coder would think were purely read-only.

But if that's the case, the code is WTF! in ways far worse than mere foolish comparisons can express.

Re: The Case for Switch

2007-04-26 09:58 • by Matthew Parent (unregistered)
Clearly the first snippet of code is meant to handle race conditions in multi-threaded code. Just because it was a comma a split second ago doesn't mean it hasn't changed so we'd better check again. It's a wonderful way of handling race conditions. Much better than those messy mutexes

Re: The Case for Switch

2007-04-26 09:59 • by Pigrew (unregistered)
133748 in reply to 133737
But, shouldn't it decide if it is a comma or a quotation the first time that I look at it?

Re: The Case for Switch

2007-04-26 10:17 • by strictnein
133750 in reply to 133748
Pigrew:
But, shouldn't it decide if it is a comma or a quotation the first time that I look at it?


That's the amazing thing about C++, sometimes it will tell you the truth, but every once in a while it will lie to you, just for kicks. That's why advanced programming techniques like the one shown come in handy. C++ will never lie twice in a row! It's part of the spec.

Re: The Case for Switch

2007-04-26 10:18 • by Jupp3 (unregistered)
At least the coder of the first example didn't do this:
for (i=0; i < strlen(buffer); i++)

:-)

Re: The Case for Switch

2007-04-26 10:21 • by snoofle
133754 in reply to 133735
Ken:
Hmm...

If it's a comma, and remains a comma, replace it with a period.

If it's still a comma, and it changes to a quote, replace it with an apostrophe.

Sometimes an assignment will fail due to a failing circuit, high tide or a wolf beying at the moon. In those rare special cases, the comma might still be a comma even after having been changed to a period.

Better safe than sorry!

Re: The Case for Switch

2007-04-26 10:23 • by diaphanein (unregistered)
133755 in reply to 133746
dkf:
if (buffer[i]==',')

{
if (buffer[i]==',') buffer[i]='.';
}
This could be OK if this was C++ and suitably defined operators were available which transmogrified the state in places that the naive coder would think were purely read-only.

But if that's the case, the code is WTF! in ways far worse than mere foolish comparisons can express.

It would still be a WTF if it was C++ and you changed the semantics of operator==. In my book, that's justification for murder.

Re: The Case for Switch

2007-04-26 10:24 • by KattMan
133757 in reply to 133740
mkb:
zWindow * child;


ZWINDOW STARCHILD

I especially like the switch that does nothing.


Hey that's my avatars name on Guild Wars! How dare you post that. Now I'm going to have to change it.

Re: The Case for Switch

2007-04-26 10:25 • by diaphanein (unregistered)
133759 in reply to 133755
diaphanein:
dkf:
if (buffer[i]==',')

{
if (buffer[i]==',') buffer[i]='.';
}
This could be OK if this was C++ and suitably defined operators were available which transmogrified the state in places that the naive coder would think were purely read-only.

But if that's the case, the code is WTF! in ways far worse than mere foolish comparisons can express.

It would still be a WTF if it was C++ and you changed the semantics of operator==. In my book, that's justification for murder.

Actually, when I look at the code again, no, this would not be ok in C++. You cannot change operator==(char, char). If those weren't builtin types, you could do something with operator== that caused a mutation to its args, but you'd still be a jackass.

Re: The Case for Switch

2007-04-26 10:26 • by snoofle
133761 in reply to 133744
Chris Walton:
Scare away newbies with a one-liner equivalent

void ReplaceCommas (char* p) {

for(; *p; p++) *p = *p == ',' ? '.' : *p;
}



CAPTCHA: muhahaha (how fitting!)

Lord! It's been ages since I've coded in "C" - you just don't get to do stuff like this (at least not with this syntax) in Java. Ah, the memories :)

Re: The Case for Switch

2007-04-26 10:29 • by Jimmy (unregistered)
133764 in reply to 133744
Chris Walton:
Scare away newbies with a one-liner equivalent

void ReplaceCommas (char* p) {

for(; *p; p++) *p = *p == ',' ? '.' : *p;
}



CAPTCHA: muhahaha (how fitting!)


WTF!? How does this account for randomly-changing memory like the original? Plus, you are not utilizing the power of nested ternary operators.

Re: The Case for Switch

2007-04-26 10:30 • by Mio (unregistered)
Eh, well, I guess you've got to make sure that that dastardly operator== method didn't mutilate your string.

Re: The Case for Switch

2007-04-26 10:32 • by Shinobu (unregistered)
133766 in reply to 133744
Is that for statement necessary?
void ReplaceCommas (char* p) {

while(*p++ = *p == ',' ? '.' : *p);
}
Wouldn't that work too? The ++ should get executed after the rest of the expression, so it could equally well be on the second *p, I guess.

Re: The Case for Switch

2007-04-26 10:37 • by none (unregistered)
who knows, maybe the buffer is volatile. better check twice or thrice to be sure(tm).

Re: The Case for Switch

2007-04-26 10:41 • by loose (unregistered)
Sorry, can someone explain what's wrong with the second one? As far as I can see there's an unused switch statement that either used to do something or is maybe a placeholder for some thing in the future, but why is it a wtf?

Re: The Case for Switch

2007-04-26 10:42 • by Remco Gerlich (unregistered)
The first one is obvious; it's highly multi-threaded code, which leads to obscure race conditions.

And the programmer decided to use that phenomenon towards his evil intentions.

Change commas into periods, except if it was changed in between the two checks. Perfectly normal business rule.

Re: The Case for Switch

2007-04-26 10:43 • by snoofle
133770 in reply to 133755
diaphanein:
dkf:
if (buffer[i]==',')

{
if (buffer[i]==',') buffer[i]='.';
}
This could be OK if this was C++ and suitably defined operators were available which transmogrified the state in places that the naive coder would think were purely read-only.

But if that's the case, the code is WTF! in ways far worse than mere foolish comparisons can express.

It would still be a WTF if it was C++ and you changed the semantics of operator==. In my book, that's justification for murder.

So... operator== could be defined to toggle between the value of the object and the value of the object being compared?


// start: buf[i] is ','
if (buffer[i]==',') { // toggles: buf[i] is '.'
if (buffer[i]==',') { // toggles: buf[i] is ','
buffer[i]='.'; // op= is also overloaded:
// buf[i] is FILE_NOT_FOUND
}
}

Re: The Case for Switch

2007-04-26 10:44 • by NeoMojo (unregistered)
133771 in reply to 133768
you leave stuff like that in your production code :O

Re: The Case for Switch

2007-04-26 10:45 • by Cable
133772 in reply to 133768
Nope sry it's not a placeholder for anything and as far as the older versions tell me there has never been any use of this particular switch.

Re: The Case for Switch

2007-04-26 10:46 • by dnwiebe (unregistered)
"A Real Programmer (TM) can write FORTRAN programs in any language!"

Re: The Case for Switch

2007-04-26 10:54 • by snoofle
133775 in reply to 133770
This brings to mind the first C++ system I ever worked on. It was huge, and the folks who wrote it overloaded every single operator there was in every single class; not because they needed to (named functions would have been far more comprehensible) but because they didn't know when to stop. For example:


op= - straight deep copy
op== - shallow copy (annoying as hell: use x.equals(...) to compare)
op< - load from an in-memory stream
op> - save to an in-memory stream
op<< - load from tmp file (before XML came along)
op>> - save to tmp file
op<<= - load from DB with args ultimately the where clause
op>>= - save to DB
.

...and of course, there was always the inevitable typo sending your data to God-knows-where... (sighs)

Re: The Case for Switch

2007-04-26 10:57 • by Fred (unregistered)
133776 in reply to 133744
Chris Walton:
Scare away newbies with a one-liner equivalent

void ReplaceCommas (char* p) {

for(; *p; p++) *p = *p == ',' ? '.' : *p;
}




Nah. You can do better than that. For a start:

for(; *p = (*p == ',' ? '.' : *p); p++);

Or, better, including undefined behavior:

while (*p+=(*p++==',')<<1);

Re: The Case for Switch

2007-04-26 11:04 • by mav (unregistered)
When it comes to good music, Jessica Simpson has some nice boobies...

Re: The Case for Switch

2007-04-26 11:05 • by KenW
You're all wrong. This is obviously code designed to handle old, set in their ways buffers that are resistant to change. ;-)

Re: The Case for Switch

2007-04-26 11:32 • by Derrick Pallas
133782 in reply to 133750
strictnein:
That's the amazing thing about C++, sometimes it will tell you the truth, but every once in a while it will lie to you, just for kicks. That's why advanced programming techniques like the one shown come in handy. C++ will never lie twice in a row! It's part of the spec.


You come to a fork in the code; at this branch, there are two sentinels. The specification tells you that one only lies and one only tells the truth, though it is not obvious to you which is which. One code path leads to exit(0) and one leads to exit(-1): asking only one sentinel a single question, how do you determine which branch to take?

When is a *p not a *p?

2007-04-26 11:37 • by Alan Balkany (unregistered)

void ReplaceCommas (char* p) {
while(*p++ = *p == ',' ? '.' : *p);
}


Doesn't work: The post-increment operator makes the *p on the right-hand side different than the *p on the left-hand side.

Re: The Case for Switch

2007-04-26 11:41 • by Uberbandit (unregistered)
You guys, where are you living? You see, it's a internationalization thing we have here. English "," aren't the same than spanish "," So I believe it's checking for these two. Now, the problem is when you want to include any other language, even thought I don't think asian languages use comas... do they?

CAPTCHA: Jeez Mom! For the last time, I won't BATHE I'm a WoW Guru, now leave the garage ASAP

Re: The Case for Switch

2007-04-26 11:46 • by Goplat
int DomId = f->zChildWin::ctrlId();
switch ( DomId ) {
{
default:
break;
}
Maybe zChildWin::ctrlId() returns an object where the "operator int()" has a side effect, and the switch is to get rid of the unused variable warning?

Re: The Case for Switch

2007-04-26 11:54 • by Ston (unregistered)
133789 in reply to 133768
loose:
Sorry, can someone explain what's wrong with the second one? As far as I can see there's an unused switch statement that either used to do something or is maybe a placeholder for some thing in the future, but why is it a wtf?


It does nothing. After the call to CreateMenu, or wht ever it was, the rest of the code is waste. It loops through a list and preforms some condition check on each object. If the condition is true it executes the switch that does nothing, if false it does nothing.

It loops through a collection and makes no changes. That is superior coding.

Re: The Case for Switch

2007-04-26 11:55 • by Hans (unregistered)
133791 in reply to 133776
Fred:

while (*p+=(*p++==',')<<1);


Ohhh, beautiful!

But I never want you to work on anything I get to maintain ;-)

Re: The Case for Switch

2007-04-26 12:01 • by PseudoNoise (unregistered)
Holy Frijoles, that 2nd one is mind-boggling.

This line looks like something might have been swallowed by the board, or maybe just a formatting error (parenthesis aren't consistent font):
zWindowDlistIter trav(&this->zWindow::kidslist());

Also looks like calling trav's operator() returns the current item and moves the iterator.

I suspect trav() returns a zWindow*, so the dynamic_cast is just for fun?

Re: The Case for Switch

2007-04-26 12:03 • by ammoQ
The first one reads: copy-paste meets trial-error
i.e. a newbie who can't really program takes an existing function and changes it until it does the right thing, which this one accidentally does.

Re: The Case for Switch

2007-04-26 12:13 • by calcnerd256 (unregistered)
133796 in reply to 133744
Chris Walton:
Scare away newbies with a one-liner equivalent

void ReplaceCommas (char* p) {

for(; *p; p++) *p = *p == ',' ? '.' : *p;

}




CAPTCHA: muhahaha (how fitting!)


better:
while(*p) *(p++) = *p == ',' ? '.' : *p;

captcha: quake

Re: The Case for Switch

2007-04-26 12:15 • by dreadlocks (unregistered)
133798 in reply to 133782
Derrick Pallas:
You come to a fork in the code; at this branch, there are two sentinels. The specification tells you that one only lies and one only tells the truth, though it is not obvious to you which is which. One code path leads to exit(0) and one leads to exit(-1): asking only one sentinel a single question, how do you determine which branch to take?

too easy ... slay both sentinals and pick one at random (which always returns 4) then throw an FILE_NOT_FOUND exception and exclaim "Brillant!" while simultaneously sending a SIGKILL to self (and parent/children) and throw an error box saying "process completed successfully".

Re: The Case for Switch

2007-04-26 12:20 • by PseudoNoise (unregistered)
133799 in reply to 133792
Ah, I see now, he's casting to see if each zWindow* is a zControl*. But then doing nothing with it. Awesome.

Re: The Case for Switch

2007-04-26 12:26 • by waffles (unregistered)
133802 in reply to 133776
Fred:
while (*p+=(*p++==',')<<1);

That is beautiful. I think it'd take me at least 5 minutes to figure out what that was doing if I came across it in production code (and another two hours to figure out why it was being done that way).

Re: The Case for Switch

2007-04-26 12:35 • by Devi
133807 in reply to 133783
Alan Balkany:

void ReplaceCommas (char* p) {
while(*p++ = *p == ',' ? '.' : *p);
}


Doesn't work: The post-increment operator makes the *p on the right-hand side different than the *p on the left-hand side.


It works for me:


int main()
{
char buffer[100]="Hello, my name is, what, my name is";
char *p=buffer;

// I just had to try this one out first...
while(*p+=(*p++==',')<<1);

printf("%s\n",buffer);

p=buffer;

while(*p++=(*p==','?'.':*p));

printf("%s\n",buffer);

getch();
return 0;
}


Outputs:

Hello,"my name is,"what,"my name is
Hello."my name is."what."my name is

On VC++ 2003.

It seems that in the second conversion the increment is applied to p after the assignment.

I'm still trying to not think about what's actually happening in the first loop...

Re: The Case for Switch

2007-04-26 12:37 • by Steve (unregistered)
For intentional abuse of the switch statement, I refer you to Duff's Device, one of the most diabolical hacks ever conceived, even more so in that it apparently works and is valid C.

I once attended a lecture given by the aforementioned Tom Duff at SIGGRAPH many years ago. His lecture was somewhat rambling and I don't think completely prepared. He ended the lecture prematurely with the words "Let's abort this pig" and walked off stage.

An amusing and brilliant, if somewhat disorganized, character.

Re: The Case for Switch

2007-04-26 12:48 • by EmmanuelD (unregistered)
meh @ second code snippet.

Better worded as: "how to make a non-trivial do_nothing() function".

The captcha summariez everything (again. Do you have some kind of predictive algorithm?): ewww.
« PrevPage 1 | Page 2Next »

Add Comment