- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
I am first. Am I not? Akismet, whatever you are. Damn Yuo.
Admin
On a more serious note, simple If conditions might not delight the programmer. So he writes switches.
Admin
and if statements?
Admin
Admin
Slow day in the wtf community. Maybe next we can talk about how somebody used a variable named 'ret' for a return value.
Admin
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.
Admin
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!
Admin
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.
Admin
Agreed. I missed my first comment twice.
Admin
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.
Admin
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.)
Admin
That's cos of timeszone dear.
Admin
Better safe than sorry.
Admin
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!
Admin
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
Admin
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.
Admin
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.
Admin
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?
Admin
TRWTF is realy
Admin
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.
Admin
No default in the switch? What if there suddenly is a new readystate? What then??
Admin
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.
Admin
I'm a little worried about how many people are missing the problem with this code.
Admin
Admin
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
Admin
Wonder if there's a reason for the fall-trough cases.
Admin
This is exactly why JavaScript needs an assert statement, then you could add something like:
Admin
Admin
Assuming a single threaded application there is no fallthrough, you european newbie.
Admin
The coder obviously forgot the for loop!
Admin
Your suggestion is a WTF in and of itself.
Admin
And not being Hitler isn't yours, is it? Stop being racist, dude, calm down.
Admin
Me too.
Anyway, those "break", are breaking the "if" not the switch, so this code is running all the if's before ending.
Admin
Admin
There actually is a 5.
And that's connection code.
TRWTF is no case 5: break;
Admin
Godwin's Law just says that as an internet argument progresses, the odds of a Hitler comparison approach 1.
Admin
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.
Admin
Admin
Admin
Admin
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.
Admin
Captcha: damnum - how appropriate.
Admin
Which is why I never use case, or break for that matter. This pattern works better:
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?
Admin
Admin
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.
Admin
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.
Admin
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.
Admin
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
Admin
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.
Admin
if(true) continue; //play it again sam!