• (cs)

    I am first. Am I not? Akismet, whatever you are. Damn Yuo.

  • (cs)

    On a more serious note, simple If conditions might not delight the programmer. So he writes switches.

  • bjeeb (unregistered) in reply to Nagesh

    and if statements?

  • (cs)
    bjeeb:
    *and* if statements?
    You never know what is going to happen to that pesky readyState between the case and the if. :)
  • (cs)

    Slow day in the wtf community. Maybe next we can talk about how somebody used a variable named 'ret' for a return value.

  • (cs)

    I was going to post a witticism about how TRWTF is that they check for a 200 status and not a 2**/3**, but apparently if I quote the code from the article then Akismet throws a strop.

    TRWTF is Akismet.

  • (cs)

    This is defensive programming at its best! If readyState changes shortly after the switch, the if will catch it! And if the JavaScript specification removes the switch feature, then he already has a replacement If-Else framework set up to handle the code changes!

  • trwtf (unregistered) in reply to Nagesh
    Nagesh:
    I am first. Am I not? Akismet, whatever you are. Damn Yuo.
    Indian coders are always first but you have to ask yourself - do you want it done fast or do you want it done right??? I'm guessing your code is about as useful as your comment.

    As for today's WTF, I've seen people make superfluous checks plenty of times before but I can't recall ever seeing code that checks the same variable twice on the same line. That's... interesting.

  • (cs) in reply to JuanCarlosII
    JuanCarlosII:
    I was going to post a witticism about how TRWTF is that they check for a 200 status and not a 2**/3**, but apparently if I quote the code from the article then Akismet throws a strop.

    TRWTF is Akismet.

    Agreed. I missed my first comment twice.

  • Jason Hooper (unregistered)

    This is obviously not production code. If it was, you wouldn't still have alert()'s displaying the responseText every time the state went to 4. This is dev code and perhaps the coder is actually going to do something with the other states. So this probably isn't a true dailywtf, but the concept still is a bit funny.

  • pWord (unregistered)

    The double check is obviously redundant, but it's not a big deal and I can easily see how it could sneak in. (Copy-paste some code, a hasty re-write...)

    And while all the switch-case stuff may seem like a waste of space, it might be nice to have if there are plans to deal with other cases in the future. (I haven't handled XmppRequests manually myself, only using JQuery, so I don't know whether the other states are ever useful.)

  • (cs) in reply to trwtf
    trwtf:
    Nagesh:
    I am first. Am I not? Akismet, whatever you are. Damn Yuo.
    Indian coders are always first but you have to ask yourself - do you want it done fast or do you want it done right??? I'm guessing your code is about as useful as your comment.

    As for today's WTF, I've seen people make superfluous checks plenty of times before but I can't recall ever seeing code that checks the same variable twice on the same line. That's... interesting.

    That's cos of timeszone dear.

  • Demo (unregistered)

    Better safe than sorry.

  • njsg (unregistered)

    Also, what if httpReq.responseText changes between the assignment and dataInsert?

    Setting a variable, and then use it only sometimes qualifies as WTF, but in this case the WTF is that this really look like two people wrote different solutions at the same time and someone had fun doing the CVS merge — I mean, why not keep the ifs and the switch, that way everyone is going to be happy!

  • remi bourgarel (unregistered)

    What is really interesting here, is to guess how the hell this developer arrived at this point. You know these afternoon when you stay on a freaking strange bug, and you end-up doing some stupid code like this one. Maybe at first he didn't put the "break" so he said

    • why is it going through my case 4 ?
    • I have to check if the value is correct and then I'll break
  • Christopher (unregistered)

    Seriously. What's wrong with this? A couple of empty case statements? Using the alert() is worse than that. Presumably there is a readyState prior to 4, that at some point might need to be tested.

    An all around stupid post. TRWTF is this TDWTF. There must have been better submissions than this.

    CAPTCHA: facilisi. Facist pasta.

  • Mr. S. (unregistered)

    I didn't catch it at first, but each case has an identical if check. Having cases for each of the possible values of readystate is not a WTF.

  • x (unregistered) in reply to amischiefr
    amischiefr:
    Slow day in the wtf community. Maybe next we can talk about how somebody used a variable named 'ret' for a return value.

    More like slow YEAR (or two). Does anyone remember when thedailywtf.com had two posts per day? And they were good. And there were no crappy fillers like comics or Alex's soap box. Are people writing good code all of a sudden or are people afraid to contribute?

  • anon (unregistered)

    TRWTF is realy

  • Leo (unregistered) in reply to Christopher
    Christopher:
    Seriously. What's wrong with this? A couple of empty case statements? Using the alert() is worse than that. Presumably there is a readyState prior to 4, that at some point might need to be tested.

    An all around stupid post. TRWTF is this TDWTF. There must have been better submissions than this.

    CAPTCHA: facilisi. Facist pasta.

    Can't tell if you're trolling or not. In case you really missed it, the WTF is that it switches to each possibility, then checks it with an if statement again, to be doubly sure the variable hasn't changed in the course of the switch statement.

    There have certainly been better WTFs, but this one is still a bit silly.

  • Echma (unregistered)

    No default in the switch? What if there suddenly is a new readystate? What then??

  • (cs) in reply to Jason Hooper
    Jason Hooper:
    This is obviously not production code. If it was, you wouldn't still have alert()'s displaying the responseText every time the state went to 4. This is dev code and perhaps the coder is actually going to do something with the other states. So this probably isn't a true dailywtf, but the concept still is a bit funny.

    It depends on the data shown in the alert() box is. It could be "Your request number 12345 has been logged" and that message may have just been written to tblRequestLog. It's sloppy, but there's nothing wrong with alerting an AJAX response in production.

    Here are the other readyStates: 0 - The object has been created, but not initialized (the open method has not been called). 1 - A request has been opened, but the send method has not been called. 2 - The send method has been called. No data is available yet. 3 - Some data has been received; however, neither responseText nor responseBody is available. 4 - All the data has been received.

    I've never had any use for any of them but 4. I can't think of a process where the others would be actionable.

  • Scott (unregistered)

    I'm a little worried about how many people are missing the problem with this code.

  • (cs) in reply to Christopher
    Christopher:
    An all around stupid post. TRWTF is this TDWTF. There must have been better submissions than this.
    Clearly you are difficult to satisfy. When you go to a baseball game, do you criticize every batter who strikes out or hits an easy grounder? Do you yell insults at every basketball player who misses a shot? What exactly is your line here? Granted it's weaker than most, but it's still something I'd say "What the fxxx" at if I saw it in my company's code.
  • Bryan the K (unregistered)

    The real WTF is that he's using hard coded values. He should have created a variable called READY_STATE = 4

    And then checked to see if httpReq.readyState = READY_STATE

    /CAPTCHA: Saluto. I gave a saluto to my Italian comrades

  • Toster (unregistered)

    Wonder if there's a reason for the fall-trough cases.

  • (cs)

    This is exactly why JavaScript needs an assert statement, then you could add something like:

    case 4: if(httpReq.readyState == 4){
        assert(httpReq.readyState == 4, "We've been betrayed!");
        ...
    
  • trwtf (unregistered) in reply to Nagesh
    Nagesh:
    trwtf:
    Nagesh:
    I am first. Am I not? Akismet, whatever you are. Damn Yuo.
    Indian coders are always first but you have to ask yourself - do you want it done fast or do you want it done right??? I'm guessing your code is about as useful as your comment.

    As for today's WTF, I've seen people make superfluous checks plenty of times before but I can't recall ever seeing code that checks the same variable twice on the same line. That's... interesting.

    That's cos of timeszone dear.

    English isn't your strong suit, is it?

  • shanghai coder (unregistered) in reply to Toster
    Toster:
    Wonder if there's a reason for the fall-trough cases.

    Assuming a single threaded application there is no fallthrough, you european newbie.

  • morbiusreader (unregistered)

    The coder obviously forgot the for loop!

    onreadystatechange = function(){
        for (state=0; state<=4; ++state) {
            switch(httpReq.readyState){
                case 0: if(httpReq.readyState == state){
                        break;
                }
    [...]
            }
        }
    };
  • (cs) in reply to Bryan the K
    Bryan the K:
    The real WTF is that he's using hard coded values. He should have created a variable called READY_STATE = 4

    And then checked to see if httpReq.readyState = READY_STATE

    /CAPTCHA: Saluto. I gave a saluto to my Italian comrades

    Your suggestion is a WTF in and of itself.

  • empathic (unregistered) in reply to trwtf
    trwtf:
    Nagesh:
    trwtf:
    Nagesh:
    I am first. Am I not? Akismet, whatever you are. Damn Yuo.
    Indian coders are always first but you have to ask yourself - do you want it done fast or do you want it done right??? I'm guessing your code is about as useful as your comment.

    As for today's WTF, I've seen people make superfluous checks plenty of times before but I can't recall ever seeing code that checks the same variable twice on the same line. That's... interesting.

    That's cos of timeszone dear.

    English isn't your strong suit, is it?

    And not being Hitler isn't yours, is it? Stop being racist, dude, calm down.

  • (cs) in reply to Scott
    Scott:
    I'm a little worried about how many people are missing the problem with this code.

    Me too.

    Anyway, those "break", are breaking the "if" not the switch, so this code is running all the if's before ending.

  • trwtf (unregistered) in reply to empathic
    empathic:
    And not being Hitler isn't yours, is it? Stop being racist, dude, calm down.
    You're the one who brought Hitler into the conversation. Isn't there some rule about how it's the dumbest people with the weakest arguments who are the first to raise comparisons to Hitler?
  • queso (unregistered) in reply to Echma

    There actually is a 5.

    And that's connection code.

    TRWTF is no case 5: break;

  • anon (unregistered) in reply to trwtf
    trwtf:
    empathic:
    And not being Hitler isn't yours, is it? Stop being racist, dude, calm down.
    You're the one who brought Hitler into the conversation. Isn't there some rule about how it's the dumbest people with the weakest arguments who are the first to raise comparisons to Hitler?

    Godwin's Law just says that as an internet argument progresses, the odds of a Hitler comparison approach 1.

  • Freek (unregistered)

    To everyone that thinks the test on the status is redundant, think again. A request for a non-existing page will result in a readyState 4, while the response status will be 404 (not found). Or, to make it simple, the readyState is reflects the state of the request, while the status reflects the state of the response. Google for Http readystate and open the second result if you want to learn more.

  • Nagesh Kukunoor (unregistered) in reply to trwtf
    trwtf:
    empathic:
    And not being Hitler isn't yours, is it? Stop being racist, dude, calm down.
    You're the one who brought Hitler into the conversation. Isn't there some rule about how it's the dumbest people with the weakest arguments who are the first to raise comparisons to Hitler?
    +1 for identifying a case of "Reductio ad Hitlerum" (and no, it's not Godwin's Law, that's something different). I agree, as soon as you invoke Hitler your argument is completely invalid and your opinion is about as useful as an offshore coding team.
  • Nagesh Kukunoor (unregistered) in reply to anon
    anon:
    Godwin's Law just says that as an internet argument progresses, the odds of a Hitler comparison approach 1.
    He meant Reductio ad Hitlerum. It's nothing to do with Godwin's law, it's an ad-hominem attack.
  • minime (unregistered) in reply to x
    x:
    amischiefr:
    Slow day in the wtf community. Maybe next we can talk about how somebody used a variable named 'ret' for a return value.

    More like slow YEAR (or two). Does anyone remember when thedailywtf.com had two posts per day? And they were good. And there were no crappy fillers like comics or Alex's soap box. Are people writing good code all of a sudden or are people afraid to contribute?

    I recently contributed a really WTF worthy hack found in production code, but it wasn't accepted... maybe alex does not have time anymore and just picks whatever is on top in his mailbox today?

  • McKay (unregistered) in reply to shanghai coder

    Assuming a single-threaded application in AJAX is a mistake, because like the article says the tricky part is when the A comes in with the other stuff. AJAX is asynchronous and multi-threaded.

    Yes, this code is a little silly, but I imagine the developer had the status change on him twice in quick succession, and the event fired twice, and figured this was an appropriate fix.

  • RootKid (unregistered) in reply to trwtf
    trwtf:
    Nagesh:
    trwtf:
    Nagesh:
    I am first. Am I not? Akismet, whatever you are. Damn Yuo.
    Indian coders are always first but you have to ask yourself - do you want it done fast or do you want it done right??? I'm guessing your code is about as useful as your comment.

    As for today's WTF, I've seen people make superfluous checks plenty of times before but I can't recall ever seeing code that checks the same variable twice on the same line. That's... interesting.

    That's cos of timeszone dear.

    English isn't your strong suit, is it?

    [image]

    Captcha: damnum - how appropriate.

  • Gary (unregistered) in reply to ubersoldat
    ubersoldat:
    Scott:
    I'm a little worried about how many people are missing the problem with this code.

    Me too.

    Anyway, those "break", are breaking the "if" not the switch, so this code is running all the if's before ending.

    Which is why I never use case, or break for that matter. This pattern works better:

    if (case==1) {
    
    }
    else if (case==2) {
    
    }
    else {
    
    }
    

    And for breaking if and for loops: why not just write the ifs in the right order and use a while/do loop with a counter?

  • (cs) in reply to ubersoldat
    ubersoldat:
    Scott:
    I'm a little worried about how many people are missing the problem with this code.

    Me too.

    Anyway, those "break", are breaking the "if" not the switch, so this code is running all the if's before ending.

    Does "break" really break the if statements in javascript? It will break out of switch-blocks and while-blocks but not if blocks. Otherwise you wouldn't be able to write:
    while (test)
    {
    if (some_other_test)
    {
    do_something();
    break;
    }

    something_else(); }

  • empathic (unregistered) in reply to trwtf
    trwtf:
    empathic:
    And not being Hitler isn't yours, is it? Stop being racist, dude, calm down.
    You're the one who brought Hitler into the conversation. Isn't there some rule about how it's the dumbest people with the weakest arguments who are the first to raise comparisons to Hitler?

    Yo there is, but you don't know the name of it, so I am right in that you are a racist and a newbie, dear Dr. Mengele.

  • empathic (unregistered) in reply to ubersoldat
    ubersoldat:
    Scott:
    I'm a little worried about how many people are missing the problem with this code.

    Me too.

    Anyway, those "break", are breaking the "if" not the switch, so this code is running all the if's before ending.

    If that would be the case, what would be the point of a break-statement at the first place. And continue within an if-body? Would it make the if-body a while-loop?

    Spend some more time at school and don't bring up nationalist arguments.

  • airdrik (unregistered) in reply to Freek
    Freek:
    To everyone that thinks the test on the status is redundant, think again. A request for a non-existing page will result in a readyState 4, while the response status will be 404 (not found). Or, to make it simple, the readyState is reflects the state of the request, while the status reflects the state of the response. Google for Http readystate and open the second result if you want to learn more.
    You missed the fact that they use the switch to check the readyState and then used an if to check the readyState again.

    Of course if readyState changes between the switch and the if then it will miss the break statement and flow into the next case statement. At least with this setup, it won't flow into the wrong if block if it changes midway through.

    Of course this function gets called each time readyState changes, so assuming the possibility of readyState changing during the execution of this function causing it to flow between switch cases, you would end up with multiple executions running the content of certain cases multiple times - two (or more) alerts and two (or more) dataInsert calls if readyState changes fast enough. And how do we prevent that? More code, obviously! Like adding some kind of framework that ensures that the code only gets executed once by a single request.

  • zdux (unregistered) in reply to amischiefr

    Here you go: My manager "fixed" my code in a wtfy way. The problem was that in IE printing a table of data which spanned more than a single page would reprint the same data starting on the second page.

    The webpage looked like this: [header] [table] row 1 row 2 row 3 row 4 [! -- page break would be here--!] row 5 row 6 [/table] [footer] Then the output when printed would look like this [header] [table] row 1 row 2 row 3 row 4 [! -- page break --!] row 1 row 2 row 3 row 4 row 5 [! -- page break --!] row 6 [/table] [footer]

    The apparent cause of this was the style attributes used on <td's and <tr's. Remove them and the problem disappears, along with the style. The style was generated on the client side as the data was retrieved with ajax. The style's only purpose was to color every other row and to add a horizontal line to visually separate the data. This had to do with business logic. And "No" this line does not correspond to the page break. It was simply a border-top: 2px solid red. This would usually occur around row 2 or 3.

    My advice to fix this was to use firefox. If IE is really that screwed up internally then it isn't worth using. The webpage is a report used by 3 managers and according to the logs the page was only viewed twice in the last month.

    However trying to be kind to the next programmer that was tasked with this bug I logged an svn comment explaining a way to fix this:

    Detect browser if IE then add a print button the print button navigates to a page page_ie.html which does everything the regular page does minus the ajax, using classes instead of styles. The BL logic of the horizontal line and row colors would be calculated on the server.

    Managers solution: add a hidden field on button click grab Entire HTML blob. Stuff blob into hidden field then post blob to server On server treat blob as XML file with a ridged format (meaning blob is expected to have <tr's and <td's with style attributes else crash - so don't click that print button too soon) Looping through XML remove attributes Determine where line is insert new blank row with red background color. Return blob to client Use client to strip <html <head <meta <style <script and <body tags Replace html with blob

    I fully suspect the reason this guy had been promoted to manager was to keep him out of the code *sigh

  • empathic (unregistered) in reply to airdrik
    airdrik:
    Freek:
    To everyone that thinks the test on the status is redundant, think again. A request for a non-existing page will result in a readyState 4, while the response status will be 404 (not found). Or, to make it simple, the readyState is reflects the state of the request, while the status reflects the state of the response. Google for Http readystate and open the second result if you want to learn more.
    You missed the fact that they use the switch to check the readyState and then used an if to check the readyState again.

    Of course if readyState changes between the switch and the if then it will miss the break statement and flow into the next case statement. At least with this setup, it won't flow into the wrong if block if it changes midway through.

    Of course this function gets called each time readyState changes, so assuming the possibility of readyState changing during the execution of this function causing it to flow between switch cases, you would end up with multiple executions running the content of certain cases multiple times - two (or more) alerts and two (or more) dataInsert calls if readyState changes fast enough. And how do we prevent that? More code, obviously! Like adding some kind of framework that ensures that the code only gets executed once by a single request.

    Or use a local variable to store the numeric code?

    Whoa. This site is full of european retards, hail USA, hail. God save the USA, may the green cards go out of stock soon.

  • airdrik (unregistered) in reply to empathic
    empathic:
    ubersoldat:
    Scott:
    I'm a little worried about how many people are missing the problem with this code.

    Me too.

    Anyway, those "break", are breaking the "if" not the switch, so this code is running all the if's before ending.

    If that would be the case, what would be the point of a break-statement at the first place. And continue within an if-body? Would it make the if-body a while-loop?

    Spend some more time at school and don't bring up nationalist arguments.

    if(true) continue; //play it again sam!

Leave a comment on “Sturdy Switch”

Log In or post as a guest

Replying to comment #:

« Return to Article