- 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
What's so bad abouth this? Oh... SelMA is copyright infringement, I get it.
Admin
At least he doesn't face the same problems as in http://thedailywtf.com/forums/37391/ShowPost.aspx !
Admin
Clearly the WTF is that he should have used a big 50-level nested IF/ELSE to set the various "sel" variables !
Admin
<FONT color=#ff0000>Should it not have been <%=SelAL%>instead of <=SelAL%> or is that just an error in transcription?</FONT>
<FONT color=#ff0000>[ED: my bad ... I was having difficulties syntax hilighting it and those got chopped off. It's fixed now]</FONT>
Admin
I don't see the problem, he's just separating the application logic from the presentation. I'm assuming that he was planning to move the select into a separate template file.
Admin
Obviously he had to write the code this way because "DC" is different from "D.C."!
Admin
I can't believe they hard coded these. If they change the abbreviations you're screwed. ;^)
Admin
The problem is that this is a horribly inefficient way of going about it. Let's look at a more efficient way (JScript syntax, because I can't do VBScript):
<font size="3">I hope you can appreciate the difference in length.</font>
Admin
you missed DC = D.C.
Admin
and the quotes around the state of "..."
Admin
You could make the function dynamic further and use two arrays (display, values) and the selected value, allowing the display names to differ from the values.
Admin
This is nothing. I had to deal with code like this that was used to parse XML. There wre a couple dozen booleans: one for each element type. When the beginning of the element was reached, it's corresponding boolean was marked true. When the next text element was processed, the boolean was used to determine where the went in the Object model using is long if-else statement and the boolean was set back to false. Now, consider the case where you have an empty element...
The worst part was that this was an API I written by another group in the company. Finally I just said, 'give me the code' and started maintaining it. It was the only way we could get it to work.
Admin
<FONT face="Courier New" size=2>when america splits off into the two nations, "the united states of canada" and "jesusland", this code will be difficult to maintain.</FONT>
<FONT face="Courier New" size=2>especially if the libertarians end up taking over alaska or maine and secede from the union [0]. sorry to bring that up again, btw - couldn't resist.</FONT>
<FONT face="Courier New" size=2>[0] http://www.freestateproject.org/</FONT>
Admin
There are definitely more elegant solutions for this task, but I've seen far worse than that.
Admin
If Alaska seceded, who will build their billion dollar bridges to nowhere?
Admin
<font size="2">ugh. nasty.
Even given the lack of elegance,</font>
Admin
I never thought I would say this, but a Select Case would almost be useful here.
Admin
true, but I doubt you would notice the difference on a Pentium 133 or faster.
Admin
On a busy site where this page is hit heavily...I bet you would notice.
Why execute 50 session variable retrievals & string (actually variant) comparisons when 1 will do?
Admin
If this busy web site is so heavily hit that you notice the difference, how is it going to do the processing in the next steps? After State selection, there is likely something like: order entry, order processing etc. which will definitely cause much more work than 50 hashtable lookups and string comparisons.
Admin
State of the art brillance [:)]
Admin
Oh, I've seen way better code than this. Applying the paradigm from the code I have to maintain, the above sample would look like this:
<select class="f" name="State">
<option value>State</option>
<option value="AL">AL</option>
<option value="AK">AK</option>
<FONT color=#008000>.. snip...</FONT>
<option value="WI">WI</option>
<option value="WV">WV</option>
</select>
<FONT color=#000000><script></FONT>
<FONT color=#000000>document.f.value = <FONT style="BACKGROUND-COLOR: #ffff00"><%</FONT>=Session("State")<FONT style="BACKGROUND-COLOR: #ffff00">%></FONT></FONT>
<FONT color=#000000></script></FONT>
<FONT color=#000000></FONT>
Admin
Actually that should say "document.State.value", but you get the idea.
Admin
It's not 50. It's 50 x [number of hits]/sec.
Anyway, are you actually defending this code? With almost no effort, this could be 50 times more efficient. Are you saying "don't bother"?
Admin
Admin
Admin
It's 50 for each hit. But for each hit, there will be a lot of additional work. Processing a lot of other input, database lookups, writing something to the database etc.
I'm not defending it in a "don't bother" fashion, but compared to the "random generator" of the previous WTF its a minor annoyance.
Admin
How exactly do you think "else if" or select/case would reduce the comparisons to 1? If the cases
were evenly distributed, you'd get 25 comparisons on average, by moving more populous states
to the front you might get it down to 5 or so. Given the small size of the strings, that might even
be faster than the hash-based solution.
But speed isn't the issue here, code bloat is. The hash-based solution is the the right way to do it,
because it doesn't duplicate code.
Admin
You're right, of course; it won't be reduced to 1, but it certainly would be better.
My point is not to fully rewrite it, but to point out that simply by using basic language features, it could be optimzed greatly.
There's no question the whole thing should be chucked and redesigned.
Admin
Using the >= operator and nested IFs, you could get it down to 6 comparisons in every case; though it would not reduce code bloat and be less readable.
Admin
Oh. My. Dawg.
I now have to go wash my eyes with soap. Damn you!
dZ.
Admin
They even dedicated the name of CSS class referenced in the select list after their grade on this assignment - "f".
Admin
You forgot the 3rd half: The Unidos States de Mexico.
Admin
LOL, I think it should be called JeebusLand.
Admin
divertido, pero nosotros hay ya
Admin
I have seen that exact same code but only JSP.
</select>Of course when we pointed out all the ifs, our JSP 'developer' circumvented the if's by adding..
Of course earlier in the code this was put in...
String stateValue = session("state") == null ? "" : session("state");
Admin
He, he, he. FYI: Yo hablo español, pero pensé que sería más divertido para la mayoría de los anglo-parlantes mezclar las lenguas en el nombre. :)
dZ.
Admin
<FONT face="Courier New" size=2>por favor, deja de abusar mi idioma. gracias!</FONT>
Admin
¿A le se permite estar aquí? ¿puedo ver su tarjeta verde?
Admin
<FONT face="Courier New" size=2>ah, muy bien. ayudame entender: porque es que a los latinos le gusta agregar ASCII a los nombres de AIM, ICQ, etc. (fig 12)? todos mis primos en venezuela tienen nombres asi.</FONT>
<FONT face="Courier New" size=2>fig 12: ·#·$2(¯`•._.•·$PENDEJO·$2•._.•´¯)·$1Q</FONT>
Admin
The FreeStateProject already chose New Hampshire.
Admin
<FONT face="Courier New" size=2>dude, are you from brazil? or is this a case of SASL?</FONT>
Admin
Oh, posh. Next you'll be telling me that it's a mistake to hard code every zip code?
It was a lot of work, but by god, it was worth it.
Admin
I have seen that exact same code but only JSP.
Of course when we pointed out all the ifs, our JSP 'developer' circumvented the if's by adding..
<select class="f" name="State">
<% if (!session("state").equals("")) { %>
<option value="<%=session("state")%>" selected><%=session("state")%>
<% } %>
<option value>Stateoption>
<option value="AL">ALoption>
...
Of course earlier in the code this was put in...
String stateValue = session("state") == null ? "" : session("state");
Admin
wtf? El Babelfisho?
dZ.
Admin
En mi experiencia, esas mariconadas cruzan fronteras culturales.
-dZ.
Admin
Lo hablo como segunda lengua. Puedo mecanografiarla cuando pienso de ella pero no soy muy bueno cuando la hablo.
Admin
USA, I learned some in school but most from a book. I suck don't I? haha
Admin
Hard coding every zip code is not a mistake if you keep a hard coded list of the particular city to which they belong.
dZ.
Admin
You're under the impression that a switch/select statement gets reduced to a linear set of "else if" cases. Luckily, you're not the one implementing our compilers, or we would all be screwed. A good compiler usually generates a branch or jump table. Implemented as a binary tree, this would mean O(log n) comparisons. That's 3.9 average comparisons in our 50 states (4 comparisons in the worst case).