• (cs) in reply to cthulhu
    cthulhu:
    You could define how bad a piece of code is as proportional to the likihood of it of wasting a lot of your time in the future. I don't find this piece of code to be bad under that definition.

    I wish you weren't right. You are, though. I spend a lot of time maintaining code like this - code that could have been written right the first time. You can't justify the rewrite because the rewrite would take longer than the occasional quick patch, and a rewrite would require more debugging. You can argue that maintaining code like this just creates an increasingly worse situation that gets harder to maintain in the future, but after a couple years of making that same argument, it becomes less effective. So what if it's slow. Did someone complain about it being slow?

  • aardvark (unregistered)

    I can reduce it to 1 line.

    function submitTrap3(){ var n = 0; var f =0; var elementItem = ""; if (window.event.keyCode == 13) { for (n=0;n<document.forms[0].elements.length;n++) { elementItem = document.forms[0].elements[n].name; if ((document.forms[0].elements[n].value != "") && (elementItem == trim(document.forms[0].txtControlName.value))) { switch (elementItem) { case "header1:SearchBox" : { __doPostBack('header1:goSearch',''); break; } case "Text1": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text2": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text3": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text4": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text5": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text6": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text7": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text8": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text9": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text10": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text11": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text12": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text13": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text14": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text15": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text16": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text17": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text18": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text19": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text20": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text21": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text22": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text23": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text24": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text25": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text26": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text27": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text28": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text29": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text30": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text31": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text32": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text33": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text34": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text35": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text36": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text37": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text38": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text39": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text40": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text41": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text42": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text43": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text44": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text45": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text46": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text47": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text48": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text49": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text50": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } } } } }}

  • (cs)

    The only thing missing from this snippet is the one special case in the middle with an extremely subtle one-character variation or bug.

    Or maybe there already is one - my eyes glazed over after the first 25 or so cases.

  • (cs) in reply to aardvark

    The problem, of course, is useless variable names. "Text50"? psshh... More like "NameOfSecondCousinsFirstDeceasedMotherInLaw".

  • anonymous (unregistered)

    TRWTF is that the article preview on the front page, takes up more space than most complete articles.

  • mauve (unregistered)

    TRWTF is that window.event.cancel isn't even right. It's supposed to be window.event.cancelBubble.

    Also window.event only works in IE's non-standard event API. Other browsers use DOM Level 2 events.

  • (cs) in reply to Steenbergh
    Steenbergh:
    Bombe:
    I don’t see what’s wrong with that code.

    Well, he could've made a function MoveNext() { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); }

    And call that from his switch. That's what I would've done anyway.

    In case my boss reads this, just kidding...

    OR, he could have... not put a break statement and had them all perform the same action:

    switch(foo) { case 0: case 1: case 2: //do action }

    This will let 0 1 or 2 perform the same action, so instead of a method call in each case just let them default down.

  • (cs)

    Hmmm...

    for (i=1; i<=50; i++) {
      if (strcmp(strcat("Text",sprintf("%i",i)),elementItem)) {
        window.event.returnValue=false;
        window.event.cancel = true;
        document.forms[0].elements[n+1].focus();
      }
    }
    
  • per pwn (unregistered) in reply to amischiefr

    No shit, sherlock.

  • Captain Obvious (unregistered) in reply to JoelKatz
    JoelKatz:
    I DEFY ANYONE TO DEFEND THIS CODE!
    Sure, right now it LOOKS like all 50 are processed identically - but maybe that's just coincidence! How can you be sure they'll always be processed the same way? It'd be a real shame to ASSume such a thing only to have to throw in some sort of kludge later on if one needs to be processed differently. Real programmers plan ahead, so that if a change is needed it'll be clear to even the slowest of maintenance 'developers' (like yourself, apparently ;-) exactly how to do it. That's why the pros get the big bucks!
  • (cs) in reply to aardvark
    aardvark:
    I can reduce it to 1 line.

    function submitTrap3(){ var n = 0; var f =0; var elementItem = ""; if (window.event.keyCode == 13) { for (n=0;n<document.forms[0].elements.length;n++) { elementItem = document.forms[0].elements[n].name; if ((document.forms[0].elements[n].value != "") && (elementItem == trim(document.forms[0].txtControlName.value))) { switch (elementItem) { case "header1:SearchBox" : { __doPostBack('header1:goSearch',''); break; } case "Text1": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text2": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text3": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text4": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text5": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text6": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text7": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text8": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text9": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text10": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text11": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text12": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text13": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text14": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text15": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text16": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text17": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text18": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text19": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text20": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text21": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text22": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text23": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text24": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text25": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text26": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text27": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text28": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text29": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text30": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text31": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text32": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text33": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text34": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text35": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text36": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text37": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text38": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text39": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text40": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text41": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text42": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text43": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text44": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text45": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text46": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text47": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text48": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text49": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } case "Text50": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } } } } }}

    OH God! My Eyes! My Poor Useless Eyes!!

    ~Sticky

  • Chris Ovenden (unregistered) in reply to Lennart
    Lennart:
    I love the fact that the spaces are the same in each block, so the programmer most likely copy-pasted the same code over and over again.

    Isn't that something of a relief? The thought of someone placidly typing it all out by hand just makes me want to... to... refactor.

  • Ken (unregistered) in reply to anonymous
    anonymous:
    TRWTF is that the article preview on the front page, takes up more space than most complete articles.
    this. I think the code that runs this site may well be a WTF in and of itself - good thing it can't be blamed on the site staff, seeing as it was a package they installed instead of writing a custom one (missing a perfectly good opportunity to succumb to the NIH syndrome in the process).

    captcha: no one cares what the captcha is, nor your drawn out maybe-almost-kinda-funny explanation of why something like it, mistyped, backwards, in another language is funny and related to the topic at hand.

  • MinorThread (unregistered)

    This is pathetic. Submitting fake src. This src cannot be real..

  • (cs) in reply to Hans
    Hans:
    I would expect a machine to do things like this: mechanical, straightforward, lacking inspiration, and with a pig-headed refusal to ever change its way
    The pig disgusting. The pig rattle. Rattle with dove.
  • Rob (unregistered) in reply to gosse

    Better check up on you smoke detector again there sonny.

    .... Seriously, we just finished day light savings, its a great time to do it.

  • logic (unregistered)

    // Untested!

    var ACTION_POST_BACK = 1; var ACTION_MOVE_NEXT = 2;

    var behaviour = new Array(); var nextControl = new Array();

    behaviour['header1:SearchBox'] = ACTION_POST_BACK;

    for (var i = 1; i <= 50; i++) behaviour['Text' + i] = ACTION_MOVE_NEXT;

    var formElements = document.forms[0].elements;

    for (var n = 0; n < formElements.length; n++) { var elementName = formElements[n].name;

    if (behaviour[elementName] == ACTION_MOVE_NEXT) nextControl[elementName] = formElements[n + 1]; }

    function submitTrap3() { if (window.event.keyCode == 13) { var elementName = document.forms[0].txtControlName.value;

    var currentValue = document.forms[0][elementName];
    
    if (currentValue == "")
      return;
    
    switch (behaviour[elementItem])
    {
      case ACTION_POST_BACK:
      {
        __doPostBack('header1:goSearch','');
        break;
      }
      case ACTION_MOVE_NEXT:
      {
        window.event.returnValue = false;
        window.event.cancel = true;
    
        nextControl[elementName].focus();
    
        break;
      }
    }
    

    } }

  • Resa (unregistered) in reply to jonnyq
    jonnyq:
    cthulhu:
    You could define how bad a piece of code is as proportional to the likihood of it of wasting a lot of your time in the future. I don't find this piece of code to be bad under that definition.

    I wish you weren't right. You are, though. I spend a lot of time maintaining code like this - code that could have been written right the first time. You can't justify the rewrite because the rewrite would take longer than the occasional quick patch, and a rewrite would require more debugging. You can argue that maintaining code like this just creates an increasingly worse situation that gets harder to maintain in the future, but after a couple years of making that same argument, it becomes less effective. So what if it's slow. Did someone complain about it being slow?

    How long would the rewrite really take? It would probably take me less than a minute to fix this code. I think the advantage of taking a full minute to fix this code, and another half hour or so to test it would outweigh the annoyance of having to look at it.

  • (cs)

    Well, we wouldn't want to have the code fire for the 50 corresponding dropdown boxes and 50 option groups, dont'cha know.

  • WhiskeyJack (unregistered) in reply to attemp1
    attemp1:
    how about this? (snip)

    I just can't see anything else that would make it smaller, expandable and easier to manage...

    That's how I would have done it too, without any more contextual information than what's given. This way if we do need to change particular cases, or (God forbid) add more, it's easy enough to pull them out of there.

  • Mizchief (unregistered) in reply to jonnyq

    In a pre-compiled app I would agree with you, but this looks an aweful lot like javascript which means that big chunk of repetive code gets transfered to the browser. On a small site with only a few users this may still be fine, but multiply that by a few thousand requests then take into account other places this style of programming is used and you have added a decent amount of bandwith drainage.

  • Wow (unregistered)

    Ah, the wonders of RAD, WYSIWYG page development in MS Word and other wonderful ideas that make life a headache for those that come after...

  • Ozz (unregistered) in reply to Code Dependent
    Code Dependent:
    Hans:
    I would expect a machine to do things like this: mechanical, straightforward, lacking inspiration, and with a pig-headed refusal to ever change its way
    The pig disgusting. The pig rattle. Rattle with dove.
    FTW.
  • (cs)

    Sometimes when you do a complex data entry form, you want the tab order to depend on what data was entered. (If Text13 = "Y" skip to Text16 otherwise go to Text14). You also sometimes want to do data validation or formatting on the data. This is especially true if the data entry form is used by clerks to enter stacks of paper, where every key stroke saved adds up. Maybe the programmer set up this block of code with the intention of doing these customisations, then the project fell behind schedule and all non-essential programming stopped being developed, or was pushed to a later release.

  • (cs)

    case "Text666": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); // TODO: Need to quit this habit, its bad for heart desease developers break; }

  • (cs) in reply to KenW
    KenW:
    Fuzzypig:
    I'm not a dev and without really looking the only word that kept screaming in my head was "regex", quickly followed by the phrase "dev team restructuring"!

    When I saw that the first thing you thought of here was the overkill of a regex, my first thought was "Thank God Fuzzypig isn't a dev!".

    You know, you're so right. Such overkill!

    function submitTrap3()
    {
        if (window.event.keyCode == 13)
        {
            var f = document.forms[0];
            for (var i = 0; i < f.elements.length; i++)
            {
                var elementItem = f.elements[i].name;
                if ((f.elements[i].value != "") && (elementItem == trim(f.txtControlName.value)))
                {
                    if ((/^Text(:?[1-9]|[1-4]\d|50)$/).test(elementItem))
                    {
                        window.event.returnValue = false;
                        window.event.cancel = true;
                        f.elements[i + 1].focus();
                    }
                    else if (elementItem == "header1:searchBox")
                    {
                        __doPostBack('header1:goSearch','');
                    }
                    else { /* empty */ }
                }
            }
        }
    }

    I should be reprimanded for such waste!

    Resa:
    How long would the rewrite really take? It would probably take me less than a minute to fix this code. I think the advantage of taking a full minute to fix this code, and another half hour or so to test it would outweigh the annoyance of having to look at it.

    I think it took a minute and a half, including testing. I know, such a waste of time...

  • (cs) in reply to Voter
    TRWTF is that we just elected a socialist as the next President... Sorry, off topic...

    so⋅cial⋅ism soh-shuh-liz-uhm] –noun 1. a broad set of economic theories of social organization advocating state or collective ownership and administration of the means of production and distribution of goods, and the creation of an egalitarian society

    hmm, state ownership of business. sounds kinda like what we did with the 700 billion dollar bailout. Who pushed that through again?

    Oh wait, Bush wasn't a socialist because he didn't give a fuck about the egalitarian society part.

  • (cs) in reply to Skizz
    Skizz:
    At a guess, and do tell me if I'm wrong, you could, maybe, shorten it by removing the { and } after each case statement? It's a shot in the dark I know but it might just cut the code size down a bit.
    Yea, why did the coder do that? It seems a little redundant... Maybe it's a Javascript thing?
  • default (unregistered)

    I wouldn't last long at this company. My fix would involve a 343 lines-of-code pay cut!

    default:
    {
        window.event.returnValue=false;
        window.event.cancel = true;
        document.forms[0].elements[n+1].focus();
        break;
    }

    I can hear the manager now:

    "You did WHAT?!! You commented out 343 lines of code from the submitTrap3 function!! How could you be so reckless!! Those lines of code are coming out of your paycheck!!"

  • 123456 (unregistered) in reply to bsaksida

    I guess the following is ok, where "offset" is the index such as:

    document.forms[0].elements[offset].value == "Text1"

    function submitTrap3()                                                                             
    {                                                                                                  
        if (window.event.keyCode == 13)                                                                
        elementItem = getElementById(trim(document.forms[0].txtControlName.value)) ;                   
        if (elementItem == "header1:SearchBox")                                                        
           __doPostBack('header1:goSearch','')                                                         
        else if (elementItem.substring(0,4) == "Text") {                                               
          window.event.returnValue=false;                                                              
          window.event.cancel = true;                                                                  
          n = parseInt(elementItem.substring(4,6));                                                    
          document.forms[0].elements[n+offset].focus();                                                     
        }                                                                                              
    }
    

    But I suppose the HTML page is also a WTF.

  • 123456 (unregistered) in reply to 123456

    (v1.1)

    function submitTrap3()                                                                           
    {
        if (window.event.keyCode == 13) {
        elementItem = document.getElementById(trim(document.forms[0].txtControlName.value)) ;                   
        if (elementItem == "header1:SearchBox")
           __doPostBack('header1:goSearch','')        
        else if (elementItem.substring(0,4) == "Text") {                                               
          window.event.returnValue=false;                 
          window.event.cancel = true;                       
          n = parseInt(elementItem.substring(4,6));                                                    
          document.forms[0].elements[n+offset].focus();                                                     
          }                                                                                              
    }
    }
    
  • Shawn MacIntyre (unregistered)

    function submitTrap3() { var n = 0; var elements = document.forms[0].elements; var element; if (window.event.keyCode == 13) { for (n=0;n<elements.length;n++) { element = elements[n]; if ((element.value != "") && (element.name == trim(document.forms[0].txtControlName.value))) { if (element.name == "header1:SearchBox") { __doPostBack('header1:goSearch',''); } else if (element.name.match(/Text\d+/)) { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); } } } } }

  • (cs) in reply to attemp1

    as already mentioned.....

    switch (elementItem) { case "header1:SearchBox": { __doPostBack('header1:goSearch',''); } break;

                    default: {
                            window.event.returnValue=false; 
                            window.event.cancel = true; 
                            document.forms[0].elements[n+1].focus(); 
                        }
                        break;
                } 
    
  • Worf (unregistered) in reply to marcan
    marcan:
    The for-switch pattern (or a variation) is actually used in commercial obfuscation products. If you disassemble, say, Apple's FairPlay, you get something like this:
    int step = MAGIC_VALUE;
    

    while(1) { switch(step-MAGIC_DELTA) { case 0: // do a couple things // set step to some other value via some obfuscated math case 1: // do a couple things // set step to some other value via some obfuscated math case 2: // do a couple things // set step to some other value via some obfuscated math case 3: // do a couple things // set step to some other value via some obfuscated math case 4: // do a couple things // set step to some other value via some obfuscated math case 5: // do a couple things return; } }

    Of course, the steps aren't actually in order. Often they implement loops using only math, excluding any ifs or other jumps - they just mathematically relate the result of the comparison to the next step value. It's like flattening each line of code to be inside a switch case, and then adding complicated code to transition from one step to another of the state machine.

    That pattern is closer to a state machine than anything else - it starts at some state, does some processing, then jumps to the next state (which is calculated via a complex path).

    It's a common design pattern used in a number of places (commonly in string parsing, but also in things where certain actions are allowed based on previous actions (or behavior of something changes depending on the current state). Think of your watch - press Set to enter "Time Set" state, which has "Set Seconds", "Set Minutes", and "Set Hours" substates. Press "Set" again and you'll get "Date Set" state.

    I would hazard to guess that the code above does something that takes several rounds to do - do state 4 first, then if there's more data, do state 2 else 3, ...

  • reverse (unregistered) in reply to attemp1
    attemp1:
    how about this?...

    ...I just can't see anything else that would make it smaller, expandable and easier to manage...

    I do hope you're not serious...

    switch (elementItem) { case "header1:SearchBox" : { __doPostBack('header1:goSearch',''); break; } default: { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } }

  • ett (unregistered) in reply to JoelKatz

    Originally there may have been, or potentially there was going to be different behavior for each case. But in the end there wasn't, and the code was never updated.

  • Anon (unregistered) in reply to attemp1
    attemp1:
    how about this?

    switch (elementItem) { case "header1:SearchBox" : { __doPostBack('header1:goSearch',''); break; } case "Text1": case "Text2": case "Text3": case "Text4": case "Text5": case "Text6": case "Text7": case "Text8": case "Text9": case "Text10": case "Text11": case "Text12": case "Text13": case "Text14": case "Text15": case "Text16": case "Text17": case "Text18": case "Text19": case "Text20":
    case "Text21":
    case "Text22":
    case "Text23":
    case "Text24":
    case "Text25": case "Text26": case "Text27": case "Text28": case "Text29": case "Text30": case "Text31": case "Text32": case "Text33": case "Text34": case "Text35": case "Text36": case "Text37": case "Text38": case "Text39": case "Text40": case "Text41": case "Text42": case "Text43": case "Text44": case "Text45": case "Text46": case "Text47": case "Text48": case "Text49": case "Text50": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } }

    I just can't see anything else that would make it smaller, expandable and easier to manage...

    How about this??

    switch(elementItem){ case "header1:SearchBox" : { __doPostBack('header1:goSearch', ''); } default: { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break;
    } }

  • ba (unregistered) in reply to Anon
    Anon:
    attemp1:
    how about this?

    switch (elementItem) { case "header1:SearchBox" : { __doPostBack('header1:goSearch',''); break; } case "Text1": case "Text2": case "Text3": case "Text4": case "Text5": case "Text6": case "Text7": case "Text8": case "Text9": case "Text10": case "Text11": case "Text12": case "Text13": case "Text14": case "Text15": case "Text16": case "Text17": case "Text18": case "Text19": case "Text20":
    case "Text21":
    case "Text22":
    case "Text23":
    case "Text24":
    case "Text25": case "Text26": case "Text27": case "Text28": case "Text29": case "Text30": case "Text31": case "Text32": case "Text33": case "Text34": case "Text35": case "Text36": case "Text37": case "Text38": case "Text39": case "Text40": case "Text41": case "Text42": case "Text43": case "Text44": case "Text45": case "Text46": case "Text47": case "Text48": case "Text49": case "Text50": { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } }

    I just can't see anything else that would make it smaller, expandable and easier to manage...

    How about this??

    switch(elementItem){ case "header1:SearchBox" : { __doPostBack('header1:goSearch', ''); } default: { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break;
    } }

    The problem with that option is that you can't be sure that Text1 .. Text50 and header1:SearchBox are the only elements in the form.

  • mike (unregistered) in reply to attemp1

    if( elementItem == "header1:SearchBox") { __doPostBack('header1:goSearch','');

    { else{ window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); }

    or if you want to preserve the original code as much as possible

    if( elementItem == "header1:SearchBox") { __doPostBack('header1:goSearch','');

    { else if ( parseInt(elementItem.substring(4)) < 51){ window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); }

    Forgive me my syntax errors, write while eating a sub sandwich.

  • Kaszu (unregistered) in reply to attemp1

    How about:

    if (elementItem == "header1:SearchBox") { __doPostBack('header1:goSearch',''); } else if (/Text[0-9]*/.match(elementItem)) { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); }

  • iMalc (unregistered)

    It's almost like nobody seems to have noticed that there's 50 entries that start with "Text" and finish with a numeric string between 1 and 50. Using 50 switch cases or if expression clauses for that is still nuts. Parse that damn int and do a range check dammit! In fact, for all we know the author would have extended it past 50 if it didn't take so much code to do!

    Well at least there's no loop unrolling of that loop.

  • (cs) in reply to JoelKatz
    JoelKatz:
    I DEFY ANYONE TO DEFEND THIS CODE!

    Shoot. That's easy. Paula wrote it.

  • (cs)

    The real WTF is the opening curlies on a line by themselves. Put them on the same line as the conditional where god intended them to be!

  • attemp1 (unregistered)

    I'm sorry, apparently i should have added <sarcasm> tags. I'd didn't think people would actually take that seriously.

  • sf (unregistered) in reply to moi
    moi:
    JoelKatz:
    I DEFY ANYONE TO DEFEND THIS CODE!
    Notice that case 'Text19' and 'Text37' are different, so it's not completely moronic code.
    Damn you, you made me look ;-)
  • Your Wrong (unregistered) in reply to attemp1

    You two should never touch a line of production code EVER!

    You have no clue!

    You don't need a Switch statement when you only have two possible conditions!

    You should be designated as permanent poster 'boys' for this site!

  • attemp1 (unregistered) in reply to Your Wrong

    /sigh

    I guess i REALLY need to work on my sarcasm.

  • Tom (unregistered) in reply to attemp1

    for (int i = 50; i >=0; i--) { if (elementItem == "Text" + i) { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } }

    Worse on performance and readability but smaller :)

  • Babuchas (unregistered)

    if(/^Text([1-4]?[0-9]|5?0)$/.test(elementItem)) { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); }

  • Voted for Mr O (unregistered) in reply to Voter
    Voter:
    TRWTF is that we just elected a socialist as the next President... Sorry, off topic...
    <irony> I agree with the troll trying to take us off topic.

    No bailout for Wall Street! No Medicare or Medicaid! No money for airline or car companies! No money for children's health care (S-CHIP)! No public funds for arenas for privately-held sports teams! </irony>

    Seriously, I defy anyone to give me a reasoned response for why socialism is so bad, and why we don't have a significant amount of socialism built into our government right now.

    SOCIALISM=BOGEY-MAN

Leave a comment on “If I've Said It Once, I've Said It Fifty Times”

Log In or post as a guest

Replying to comment #227037:

« Return to Article