• (cs)

    What's so bad abouth this?  Oh... SelMA is copyright infringement, I get it.

  • (cs) in reply to Maurits

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

  • (cs)

    Clearly the WTF is that he should have used a big 50-level nested IF/ELSE to set the various "sel" variables !

  • Aaron (unregistered) in reply to Jeff S

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

  • Jonex (unregistered)

    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.

  • (cs)
    Alex Papadimoulis:
      <option value="DC" <=SelDC%>>>D.C.<!--</SPAN-->option>
    


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

    I can't believe they hard coded these.  If they change the abbreviations you're screwed.  ;^)

  • (cs) in reply to Jonex

    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>

    <font size="3">I hope you can appreciate the difference in length.</font>

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

    %>

    <select class="f" name="State"></select>

    <font size="3">I hope you can appreciate the difference in length.</font>



    you missed DC = D.C.
  • (cs) in reply to tom
    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.

    %>

    <select class="f" name="State"></select>

    <font size="3">I hope you can appreciate the difference in length.</font>



    you missed DC = D.C.


    and the quotes around the state of "..."
  • (cs) in reply to CornedBee
    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.

    %>

    <select class="f" name="State"></select>

    <font size="3">I hope you can appreciate the difference in length.</font>



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

    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.

  • (cs) in reply to Mung Kee

    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.

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

  • (cs)

    There are definitely more elegant solutions for this task, but I've seen far worse than that.

  • (cs) in reply to emptyset

    emptyset:
    <FONT face="Courier New" size=2>especially if the libertarians end up taking over alaska or maine and secede from the union</FONT>

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

  • (cs)

    <font size="2">ugh. nasty.
    Even given the lack of elegance,</font>

    If session("State")

    <font style="font-family: times new roman;" size="2"><font size="2">Needs to be evaluated 50 times no matter what.
    else if, anyone? select/switch? these aren't advanced constructs.</font>
    </font>


  • (cs) in reply to Jeff S
    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.
  • (cs) in reply to John Smallberries
    John Smallberries:
    <font size="2">ugh. nasty.
    Even given the lack of elegance,</font>
    If session("State")

    <font style="font-family: times new roman;" size="2"><font size="2">Needs to be evaluated 50 times no matter what.
    else if, anyone? select/switch? these aren't advanced constructs.</font>
    </font>



    true, but I doubt you would notice the difference on a Pentium 133 or faster.
  • (cs) in reply to ammoQ
    ammoQ:
    John Smallberries:
    <font size="2">ugh. nasty.
    Even given the lack of elegance,</font>
    If session("State")

    <font style="font-family: times new roman;" size="2"><font size="2">Needs to be evaluated 50 times no matter what.
    else if, anyone? select/switch? these aren't advanced constructs.</font>
    </font>



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

    State of the art brillance [:)]

  • (cs)

    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> 

  • (cs) in reply to A Wizard A True Star

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

     

  • (cs) in reply to ammoQ
    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"?
  • (cs) in reply to A Wizard A True Star
    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<!--</SPAN-->option>
      <option value="AL">AL<!--</SPAN-->option>
      <option value="AK">AK<!--</SPAN-->option>

    <font color="#008000">.. snip...</font>

      <option value="WI">WI<!--</SPAN-->option>
      <option value="WV">WV<!--</SPAN-->option>
    <!--</SPAN-->select>

    <font color="#000000"><script>

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

    <font color="#000000"> <p><span style="color: rgb(0, 0, 255);"><font color="#000000"></font></span> </p></font>
    <font color="#000000"> <p>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.</p> </font>
  • (cs) in reply to A Wizard A True Star
    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<!--</SPAN-->option>
      <option value="AL">AL<!--</SPAN-->option>
      <option value="AK">AK<!--</SPAN-->option>

    <font color="#008000">.. snip...</font>

      <option value="WI">WI<!--</SPAN-->option>
      <option value="WV">WV<!--</SPAN-->option>
    <!--</SPAN-->select>

    <font color="#000000"><script>

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

    <font color="#000000"> <p><span style="color: rgb(0, 0, 255);"><font color="#000000"></font></span> </p></font>
    <font color="#000000"><br/> <br/> 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.<br/> </font>
  • (cs) in reply to John Smallberries
    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.
  • (cs) in reply to John Smallberries
    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.
  • (cs) in reply to brazzy
    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.
  • (cs) in reply to brazzy
    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.
  • (cs)

    Oh.  My.  Dawg.

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

        dZ.

  • banva (unregistered)

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

  • (cs) in reply to emptyset
    emptyset:

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


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

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

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

    LOL, I think it should be called JeebusLand. 

  • (cs) in reply to DZ-Jay
    DZ-Jay:
    emptyset:

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


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



    divertido, pero nosotros hay ya
  • John (unregistered)

    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>
    <% } %>
    <option value>State</option>
    <option value="AL">AL</option>
    ...
    </select>

    Of course earlier in the code this was put in...
      String stateValue = session("state") == null ? "" : session("state");

  • (cs) in reply to Mung Kee
    Mung Kee:
    DZ-Jay:
    emptyset:

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


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

    Mung Kee:

    divertido, pero nosotros hay ya

    <FONT face="Courier New" size=2>por favor, deja de abusar mi idioma.  gracias!</FONT>

  • (cs) in reply to emptyset
    emptyset:

    Mung Kee:

    divertido, pero nosotros hay ya

    <font face="Courier New" size="2">por favor, deja de abusar mi idioma.  gracias!</font>


    ¿A le se permite estar aquí? ¿puedo ver su tarjeta verde?
  • (cs) in reply to DZ-Jay

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

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

  • heffer (unregistered)

    The FreeStateProject already chose New Hampshire.

  • (cs) in reply to Mung Kee

    Mung Kee:

    ¿A le se permite estar aquí?

    <FONT face="Courier New" size=2>dude, are you from brazil?  or is this a case of SASL?</FONT>

  • (cs) in reply to emptyset

    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.

  • John (unregistered) in reply to 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="
    <%=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");

  • (cs) in reply to Mung Kee
    Mung Kee:
    emptyset:

    Mung Kee:

    divertido, pero nosotros hay ya

    <font face="Courier New" size="2">por favor, deja de abusar mi idioma.  gracias!</font>


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


    wtf?  El Babelfisho?

        dZ.

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

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



    En mi experiencia, esas mariconadas cruzan fronteras culturales.

        -dZ.

  • (cs) in reply to DZ-Jay
    DZ-Jay:
    Mung Kee:
    DZ-Jay:
    emptyset:

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


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

    Mung Kee:

    ¿A le se permite estar aquí?

    <font face="Courier New" size="2">dude, are you from brazil?  or is this a case of SASL?</font>



    USA, I learned some in school but most from a book.  I suck don't I?  haha
  • (cs) in reply to Volmarias
    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.

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

Leave a comment on “State Management”

Log In or post as a guest

Replying to comment #:

« Return to Article