• mav (unregistered)

    Haha, terrible, just terrible code. But funny.

  • (cs) in reply to mav

    edit. oops. misread the first one.

  • 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.

  • 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.

  • (cs)

    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.

  • 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. ;-)

  • 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.

  • (cs)
    zWindow * child;

    ZWINDOW STARCHILD

    I especially like the switch that does nothing.

  • wiregoat (unregistered) in reply to exaxe

    I have one. Not sure what it has to do with programming though.

    captcha doom. I am in a backwards mood today.

  • 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!)

  • (cs)

    Mwuahuhauhauhauha

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

  • 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.

  • 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

  • Pigrew (unregistered) in reply to bonzombiekitty

    But, shouldn't it decide if it is a comma or a quotation the first time that I look at it?

  • (cs) in reply to Pigrew
    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.

  • Jupp3 (unregistered)

    At least the coder of the first example didn't do this: for (i=0; i < strlen(buffer); i++)

    :-)

  • (cs) in reply to Ken
    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!

  • diaphanein (unregistered) in reply to dkf
    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.

  • (cs) in reply to mkb
    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.

  • diaphanein (unregistered) in reply to diaphanein
    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.

  • (cs) in reply to Chris Walton
    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 :)

  • Jimmy (unregistered) in reply to Chris Walton
    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.

  • Mio (unregistered)

    Eh, well, I guess you've got to make sure that that dastardly operator== method didn't mutilate your string.

  • Shinobu (unregistered) in reply to Chris Walton

    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.

  • none (unregistered)

    who knows, maybe the buffer is volatile. better check twice or thrice to be sure(tm).

  • 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?

  • 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.

  • (cs) in reply to diaphanein
    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
       }
    }
    
  • NeoMojo (unregistered) in reply to loose

    you leave stuff like that in your production code :O

  • (cs) in reply to loose

    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.

  • dnwiebe (unregistered)

    "A Real Programmer (TM) can write FORTRAN programs in any language!"

  • (cs) in reply to snoofle

    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)

  • Fred (unregistered) in reply to Chris Walton
    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);

  • mav (unregistered)

    When it comes to good music, Jessica Simpson has some nice boobies...

  • (cs)

    You're all wrong. This is obviously code designed to handle old, set in their ways buffers that are resistant to change. ;-)

  • (cs) in reply to strictnein
    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?

  • 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.

  • 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

  • (cs)
    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?
  • Ston (unregistered) in reply to loose
    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.

  • Hans (unregistered) in reply to Fred
    Fred:
    while (*p+=(*p++==',')<<1);

    Ohhh, beautiful!

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

  • 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?

  • (cs)

    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.

  • calcnerd256 (unregistered) in reply to Chris Walton
    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

  • dreadlocks (unregistered) in reply to Derrick Pallas
    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".
  • PseudoNoise (unregistered) in reply to PseudoNoise

    Ah, I see now, he's casting to see if each zWindow* is a zControl*. But then doing nothing with it. Awesome.

  • waffles (unregistered) in reply to Fred
    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).
  • (cs) in reply to Alan Balkany
    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...

  • 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.

  • 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.

Leave a comment on “The Case for Switch”

Log In or post as a guest

Replying to comment #133766:

« Return to Article