| « Prev | Page 1 | Page 2 | Next » |
|
Haha, terrible, just terrible code. But funny.
|
|
edit. oops. misread the first one.
|
|
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. |
|
"how many times do you have to check a condition?"
Well we know how many times he thinks he needs to check it. |
|
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.
|
|
"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. ;-) |
|
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. |
ZWINDOW STARCHILD I especially like the switch that does nothing. |
Re: The Case for Switch
2007-04-26 09:49
•
by
wiregoat
(unregistered)
|
|
I have one. Not sure what it has to do with programming though.
captcha doom. I am in a backwards mood today. |
|
Scare away newbies with a one-liner equivalent
void ReplaceCommas (char* p) {
CAPTCHA: muhahaha (how fitting!) |
|
Mwuahuhauhauhauha
The second one is so ugly, it reminds me of: syntatic sugar causes cancer of the semicolon. |
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. |
|
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)
|
|
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. |
|
At least the coder of the first example didn't do this:
for (i=0; i < strlen(buffer); i++) :-) |
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)
|
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. |
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)
|
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. |
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)
|
WTF!? How does this account for randomly-changing memory like the original? Plus, you are not utilizing the power of nested ternary operators. |
|
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)
|
|
Is that for statement necessary?
void ReplaceCommas (char* 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.
|
|
who knows, maybe the buffer is volatile. better check twice or thrice to be sure(tm).
|
|
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?
|
|
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. |
So... operator== could be defined to toggle between the value of the object and the value of the object being compared?
|
Re: The Case for Switch
2007-04-26 10:44
•
by
NeoMojo
(unregistered)
|
|
you leave stuff like that in your production code :O
|
|
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.
|
|
"A Real Programmer (TM) can write FORTRAN programs in any language!"
|
|
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:
. ...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)
|
Nah. You can do better than that. For a start: for(; *p = (*p == ',' ? '.' : *p); p++); Or, better, including undefined behavior: while (*p+=(*p++==',')<<1); |
|
When it comes to good music, Jessica Simpson has some nice boobies...
|
|
You're all wrong. This is obviously code designed to handle old, set in their ways buffers that are resistant to change. ;-)
|
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? |
|
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. |
|
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 |
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)
|
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)
|
Ohhh, beautiful! But I never want you to work on anything I get to maintain ;-) |
|
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? |
|
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)
|
better: while(*p) *(p++) = *p == ',' ? '.' : *p; captcha: quake |
Re: The Case for Switch
2007-04-26 12:15
•
by
dreadlocks
(unregistered)
|
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)
|
|
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)
|
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). |
It works for me:
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... |
|
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. |
|
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. |
| « Prev | Page 1 | Page 2 | Next » |