Comment On State Management

I look back at one of the first things I posted on The Daily WTF (tblState) and get a kick out of how naïve I used to be. Back then, most of the worst code I'd come cross would barley make an honorable mention. Back then, I would never have fathomed that code like Scott Neumann's submission even existed. Back then, I didn't fear that I will inevitably have to maintain code like this. Yeah, those were good times back then ... [expand full text]
« PrevPage 1 | Page 2 | Page 3Next »

Re: State Management

2005-08-29 14:20 • by Maurits
What's so bad abouth this?  Oh... SelMA is copyright infringement, I get it.

Re: State Management

2005-08-29 14:23 • by Razzie
42261 in reply to 42260

At least he doesn't face the same problems as in http://thedailywtf.com/forums/37391/ShowPost.aspx !

Re: State Management

2005-08-29 14:23 • by Jeff S
Clearly the WTF is that he should have used a big 50-level nested IF/ELSE to set the various "sel" variables !

Re: State Management

2005-08-29 14:26 • by Aaron
42263 in reply to 42262

Should it not have been <%=SelAL%>instead of <=SelAL%> or is that just an error in transcription?


[ED: my bad ... I was having difficulties syntax hilighting it and those got chopped off. It's fixed now]

Re: State Management

2005-08-29 14:31 • by Jonex
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.

Re: State Management

2005-08-29 14:31 • by kipthegreat
Alex Papadimoulis:
  <option value="DC" <=SelDC%>>>D.C.option>





Obviously he had to write the code this way because "DC" is different from "D.C."!

Re: State Management

2005-08-29 14:37 • by Mung Kee
I can't believe they hard coded these.  If they change the abbreviations you're screwed.  ;^)

Re: State Management

2005-08-29 14:41 • by CornedBee
42270 in reply to 42264
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):



<%
function FillSel(values, active)
{
var data = "";
for(var i = 0; i < values.length; ++i) {
data += '<option value="'+values[i]+'"';
if(values[i] == active) {
data += ' selected="selected"';
}
data += '>'+values[i]+'</option>\n';
}
return data;
}

var states = new Array("AL", "AK", ..., "WV");
// ... or load from some data source.

%>

<select class="f" name="State">
<%= FillSel(states, session["State"]) %>
</select>


I hope you can appreciate the difference in length.


Re: State Management

2005-08-29 14:43 • by tom
42271 in reply to 42270
CornedBee:
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):



<%
function FillSel(values, active)
{
var data = "";
for(var i = 0; i < values.length; ++i) {
data += '
if(values[i] == active) {
data += ' selected="selected"';
}
data += '>'+values[i]+'\n';
}
return data;
}

var states = new Array("AL", "AK", ..., "WV");
// ... or load from some data source.

%>




I hope you can appreciate the difference in length.





you missed DC = D.C.

Re: State Management

2005-08-29 14:47 • by Mung Kee
42272 in reply to 42271
Anonymous:
CornedBee:
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):



<%
function FillSel(values, active)
{
var data = "";
for(var i = 0; i < values.length; ++i) {
data += '
if(values[i] == active) {
data += ' selected="selected"';
}
data += '>'+values[i]+'\n';
}
return data;
}

var states = new Array("AL", "AK", ..., "WV");
// ... or load from some data source.

%>




I hope you can appreciate the difference in length.





you missed DC = D.C.




and the quotes around the state of "..."

Re: State Management

2005-08-29 14:52 • by Mung Kee
42273 in reply to 42270
CornedBee:
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):



<%
function FillSel(values, active)
{
var data = "";
for(var i = 0; i < values.length; ++i) {
data += '
if(values[i] == active) {
data += ' selected="selected"';
}
data += '>'+values[i]+'\n';
}
return data;
}

var states = new Array("AL", "AK", ..., "WV");
// ... or load from some data source.

%>




I hope you can appreciate the difference in length.





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.

Re: State Management

2005-08-29 15:03 • by dubwai
42274 in reply to 42273

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.

Re: State Management

2005-08-29 15:06 • by emptyset
42275 in reply to 42273

Mung Kee:
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.


when america splits off into the two nations, "the united states of canada" and "jesusland", this code will be difficult to maintain.


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.


[0] http://www.freestateproject.org/

Re: State Management

2005-08-29 15:08 • by ammoQ
There are definitely more elegant solutions for this task, but I've seen far worse than that.

Re: State Management

2005-08-29 15:13 • by dubwai
42279 in reply to 42275

emptyset:
especially if the libertarians end up taking over alaska or maine and secede from the union


If Alaska seceded, who will build their billion dollar bridges to nowhere?

Re: State Management

2005-08-29 15:18 • by John Smallberries
ugh. nasty.

Even given the lack of elegance,
If session("State")

Needs to be evaluated 50 times no matter what.
else if, anyone? select/switch? these aren't advanced constructs.





Re: State Management

2005-08-29 15:20 • by Maurits
42282 in reply to 42262
Jeff S:
Clearly the WTF is that he should have used a big
50-level nested IF/ELSE to set the various "sel" variables !




I never thought I would say this, but a Select Case would almost be useful here.

Re: State Management

2005-08-29 15:20 • by ammoQ
42283 in reply to 42281
John Smallberries:
ugh. nasty.

Even given the lack of elegance,
If session("State")

Needs to be evaluated 50 times no matter what.
else if, anyone? select/switch? these aren't advanced constructs.






true, but I doubt you would notice the difference on a Pentium 133 or faster.

Re: State Management

2005-08-29 15:27 • by John Smallberries
42284 in reply to 42283
ammoQ:
John Smallberries:
ugh. nasty.

Even given the lack of elegance,
If session("State")

Needs to be evaluated 50 times no matter what.
else if, anyone? select/switch? these aren't advanced constructs.






true, but I doubt you would notice the difference on a Pentium 133 or faster.


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?

Re: State Management

2005-08-29 15:32 • by ammoQ
42285 in reply to 42284
John Smallberries:

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?


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.

Re: State Management

2005-08-29 15:32 • by WIldpeaks
42286 in reply to 42284
State of the art brillance [:)]

Re: State Management

2005-08-29 15:34 • by A Wizard A True Star

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>


.. snip...


  <option value="WI">WI</option>
  <option value="WV">WV</option>
</select>


<script>


document.f.value = <%=Session("State")%>


</script>


 

Re: State Management

2005-08-29 15:35 • by A Wizard A True Star
42288 in reply to 42287

Actually that should say "document.State.value", but you get the idea.


 

Re: State Management

2005-08-29 15:37 • by John Smallberries
42289 in reply to 42285
ammoQ:
John Smallberries:

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?


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.


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

Re: State Management

2005-08-29 15:37 • by ammoQ
42290 in reply to 42287
A Wizard A True Star:

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>Stateoption>
  <option value="AL">ALoption>
  <option value="AK">AKoption>


.. snip...


  <option value="WI">WIoption>
  <option value="WV">WVoption>
select>



 



Much more pretty, but (in contrast to the WTF) depends on JavaScript. Some people turn it off for security (and other) reasons. Putting work from the server to the client is convenient but dangerous.

Re: State Management

2005-08-29 15:38 • by Mung Kee
42292 in reply to 42287
A Wizard A True Star:

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>Stateoption>
  <option value="AL">ALoption>
  <option value="AK">AKoption>


.. snip...


  <option value="WI">WIoption>
  <option value="WV">WVoption>
select>



 





Actually, I don't think this would work.  You'd still have to do
the comparison in JS, to find out which option matched, then set the
selectedIndex to that one.

Re: State Management

2005-08-29 15:41 • by ammoQ
42294 in reply to 42289
John Smallberries:


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


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.

Re: State Management

2005-08-29 15:48 • by brazzy
42296 in reply to 42284
John Smallberries:


Why execute 50 session variable retrievals & string (actually variant) comparisons when 1 will do?




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.

Re: State Management

2005-08-29 15:57 • by John Smallberries
42300 in reply to 42296
brazzy:
John Smallberries:


Why execute 50 session variable retrievals & string (actually variant) comparisons when 1 will do?




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.


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.

Re: State Management

2005-08-29 16:04 • by ammoQ
42303 in reply to 42296
brazzy:
John Smallberries:


Why execute 50 session variable retrievals & string (actually variant) comparisons when 1 will do?




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.




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.

Re: State Management

2005-08-29 16:06 • by DZ-Jay
Oh.  My.  Dawg.



    I now have to go wash my eyes with soap.  Damn you!



    dZ.



Re: State Management

2005-08-29 16:06 • by banva

They even dedicated the name of CSS class referenced in the select list after their grade on this assignment - "f".

Re: State Management

2005-08-29 16:10 • by DZ-Jay
42308 in reply to 42275
emptyset:

when
america splits off into the two nations, "the united states of canada"
and "jesusland", this code will be difficult to maintain.




You forgot the 3rd half:  The Unidos States de Mexico.


Re: State Management

2005-08-29 16:10 • by endothermal
42309 in reply to 42275
emptyset:

Mung Kee:
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.


when america splits off into the two nations, "the united states of canada" and "jesusland", this code will be difficult to maintain.


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.


[0] http://www.freestateproject.org/



LOL, I think it should be called JeebusLand. 

Re: State Management

2005-08-29 16:15 • by Mung Kee
42310 in reply to 42308
DZ-Jay:
emptyset:

when
america splits off into the two nations, "the united states of canada"
and "jesusland", this code will be difficult to maintain.




You forgot the 3rd half:  The Unidos States de Mexico.





divertido, pero nosotros hay ya

Re: State Management

2005-08-29 16:17 • by John
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>Stateoption>
<option value="AL">ALoption>
...




Of course earlier in the code this was put in...

  String stateValue = session("state") == null ? "" : session("state");



Re: State Management

2005-08-29 16:18 • by DZ-Jay
42313 in reply to 42310
Mung Kee:
DZ-Jay:
emptyset:

when
america splits off into the two nations, "the united states of canada"
and "jesusland", this code will be difficult to maintain.




You forgot the 3rd half:  The Unidos States de Mexico.





divertido, pero nosotros hay ya




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.

Re: State Management

2005-08-29 16:19 • by emptyset
42314 in reply to 42310

Mung Kee:

divertido, pero nosotros hay ya


por favor, deja de abusar mi idioma.  gracias!

Re: State Management

2005-08-29 16:23 • by Mung Kee
42315 in reply to 42314
emptyset:

Mung Kee:

divertido, pero nosotros hay ya


por favor, deja de abusar mi idioma.  gracias!



¿A le se permite estar aquí? ¿puedo ver su tarjeta verde?

Re: State Management

2005-08-29 16:24 • by emptyset
42316 in reply to 42313

DZ-Jay:

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. :)


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.


fig 12: ·#·$2(¯`•._.•·$PENDEJO·$2•._.•´¯)·$1Q

Re: State Management

2005-08-29 16:24 • by heffer

The FreeStateProject already chose New Hampshire.

Re: State Management

2005-08-29 16:26 • by emptyset
42318 in reply to 42315

Mung Kee:

¿A le se permite estar aquí?


dude, are you from brazil?  or is this a case of SASL?

Re: State Management

2005-08-29 16:26 • by Volmarias
42319 in reply to 42314
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.

Re: State Management

2005-08-29 16:26 • by John
42320 in reply to 42312
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");

Re: State Management

2005-08-29 16:29 • by DZ-Jay
42321 in reply to 42315
Mung Kee:
emptyset:

Mung Kee:

divertido, pero nosotros hay ya


por favor, deja de abusar mi idioma.  gracias!



¿A le se permite estar aquí? ¿puedo ver su tarjeta verde?




wtf?  El Babelfisho?



    dZ.



Re: State Management

2005-08-29 16:31 • by DZ-Jay
42322 in reply to 42316
emptyset:

DZ-Jay:

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. :)


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.


fig 12: ·#·$2(¯`•._.•·$PENDEJO·$2•._.•´¯)·$1Q





En mi experiencia, esas mariconadas cruzan fronteras culturales.



    -dZ.



Re: State Management

2005-08-29 16:32 • by Mung Kee
42323 in reply to 42313
DZ-Jay:
Mung Kee:
DZ-Jay:
emptyset:

when
america splits off into the two nations, "the united states of canada"
and "jesusland", this code will be difficult to maintain.




You forgot the 3rd half:  The Unidos States de Mexico.





divertido, pero nosotros hay ya




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.




Lo hablo como segunda lengua. Puedo mecanografiarla cuando pienso de
ella pero no soy muy bueno cuando la hablo.

Re: State Management

2005-08-29 16:35 • by Mung Kee
42324 in reply to 42318
emptyset:

Mung Kee:

¿A le se permite estar aquí?


dude, are you from brazil?  or is this a case of SASL?





USA, I learned some in school but most from a book.  I suck don't I?  haha

Re: State Management

2005-08-29 16:36 • by DZ-Jay
42325 in reply to 42319
Volmarias:
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.




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.



Re: State Management

2005-08-29 16:43 • by joe_bruin
42326 in reply to 42296
brazzy:
John Smallberries:


Why execute 50 session variable retrievals & string (actually variant) comparisons when 1 will do?




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.




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

« PrevPage 1 | Page 2 | Page 3Next »

Add Comment