• John Shore George Handels Fredricks Trumpter (unregistered) in reply to attemp1

    How about this? if (elementItem = "header1:SearchBox") { .... __doPostBack('header1:goSearch',''); } else { if(elementItem.substr(0, 4) == "Text") { ... int num = Convert.ToInt16(elementItem.substr(4)); ... if((num > 1) && (num < 50)) ... { ..... window.event.returnValue=false; ..... window.event.cancel = true; ..... document.forms[0].elements[n+1].focus(); ... } } }

  • John Shore George Handels Fredricks Trumpter (unregistered) in reply to John Shore George Handels Fredricks Trumpter

    Replace > sign above with >= and < with <=.

  • eval (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": //(and so on) 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...

    It's JavaScript, so:

    var Code =
      [
        'switch (elementItem)',
          '{',
            'case "header1:SearchBox":',
              '{',
                "__doPostBack('header1:goSearch','');",
                'break;',
              '}'
      ];
        for (var i = 0; i < 50; i++)
          {
            Code.push('case "Text' + (i+1) + '":');
          }
        Code.push('{');
        Code.push('window.event.returnValue=false;');
        Code.push('window.event.cancel = true;');
        Code.push('document.forms[0].elements[n+1].focus();');
        Code.push('break;');
        Code.push('}');
        Code.push('}');
        eval(Code.join("\n"));

    Much simpler...

    (Sadly, the array stuff is actually a performance hack - almost every JavaScript engine is horrible at string concatenation, and Array.join() is faster. Under Internet Explorer, it's much faster - as in, tenths of a second versus 10s of seconds.)

  • koistinen (unregistered) in reply to attemp1

    If you give up expandability and simplicity you could make it smaller. But you will probably lose some error checking as well.

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

  • Flash (unregistered)

    One of the best ways to get your code onto the Daily WTF is to overthink or underthink.

    The bottom line from what I am seeing from this code is that you are checking TWO conditions not 51. (It's either the searchbox or one of the Textboxes (we won't go into the idiocy of naming them Text1-50)

    So, to pseudocode: IF (elementitem="header1:SearchBox") { __doPostBack('header1:goSearch',''); break; } ELSE

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

    Unless there are conditions that are not mentioned here, this should work.

  • (cs) in reply to Flash
    Flash:
    One of the best ways to get your code onto the Daily WTF is to overthink or underthink.

    The bottom line from what I am seeing from this code is that you are checking TWO conditions not 51. (It's either the searchbox or one of the Textboxes (we won't go into the idiocy of naming them Text1-50)

    You might want to think some more. Assuming Text1-50 cases are the same, you actually have three possibilities.

  • Peter (unregistered) in reply to Kyre
    Kyre:
    Obviously, TRWTF is that it isn't forward thinking enough to consider the possibility of 60 results.

    Sure there is, you can always add new lines!

  • supersine (unregistered)

    you just need 2 case statements u brainfucks! No go figure how, cause I won't tell you, because you won't learn anything.

  • John Shore George Handels Fredricks Trumpter (unregistered) in reply to Flash

    In your else condition you are assuming that text boxes 1 to 50 are the only text boxes there are. What if there is a text box text343 for which this test shouldn't be applied?

  • Lyle (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...
    You can compact it.

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

    There, much smaller!

  • rleibman (unregistered) in reply to JoelKatz

    Ok ... trying hard, but... Maybe it was code written in preparation for filling it in. Maybe the code that is supposed to be different on each case stmt is yet to be written? It's still an ugly read, but that is one possibility.

  • James C. (unregistered)

    Well this is easy...

    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))) { if(elementItem=="header1:SearchBox") { __doPostBack('header1:goSearch',''); } var eiPrefix=elementItem.indexOf("Text"); var eiNumber=elementItem.replace("Text",""); if(eiPrefix>-1 && (0<eiNumber<=50)){ window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); } } } } }

    This is the basics of it. May take a couple of syntax tweaking since I just now quickly typed it before leaving work.

  • (cs) in reply to DaveAronson
    DaveAronson:
    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.
    That could just be consistent style. There are probably^Wcertainly "developers" out there stupid enough to type that all in by hand.
    Awesome! My [strike] tag dependency is no more! :D
  • (cs) 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.

    Yeah, a lot of times it's easier to copy/paste than it is to think.

  • logic (unregistered) in reply to logic

    I posted this earlier but all my formatting disappeared. Turns out there's a BBCode for that :-)

    // 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;
          }
        }
      }
    }
    

    By pre-loading a state transition array, the actual code is reduced (down to the two real conditions), performance issues are eliminated, and the code is kept very flexible. If the tab-order needs to be changed, all that is needed is to reorder the items in the nextControl associative array (though the current initialization for it is blissfully simple). If different actions are needed for certain controls, those can be placed into the behaviour associative array, and the 'switch' updated with just one 'case' per behaviour, rather than per control. With this architecture, hashing is used to locate the state transition information, which means O(lg n) performance (no loops in submitTrap3!), instead of scanning the entire form every time doing time-consuming string comparisons. I dare say this code would perform acceptably on a 486! :-)

    In the preview, the code is all double-spaced. Hopefully that's just an artefact of the preview mode... =P

  • (cs) in reply to Fuzzypig
    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"!
    Some people, when confronted with a problem, think "I know, I'll restructure the dev team." Now they have two problems.

    No, wait, that can't be right.

    How do you propose "restructuring the dev team?" With a hammer? When you only have a hammer ...

  • (cs)
    function submitTrap3()
    {
        function focus(n)
        {
             window.event.returnValue = false;
             window.event.cancel = true;
             document.forms[0].elements[n+1].focus();
        }
    
        function post_back()
        {
            __doPostBack("header1:goSearch", "");
        }
    
        var KEYCODE_CARRIAGE_RETURN = 13;
        var NUM_FOCUSES = 50;
        var POST_BACK = 0, FOCUS = 1;
        var callbacks = [post_back, focus];
        var controlName = trim(document.forms[0].txtControlName.value);
        var elements = [["header1:SearchBox"],[]];
    
        if(window.event.keyCode != KEYCODE_CARRIAGE_RETURN)
            return;
    
        for(var j=0; j<NUM_FOCUSES; j++)
            e[FOCUS][j] = "Text" + (j + 1);
    
        <span style="color:blue;">for(var i=0; i<elements.length; i++)
        {
            <span style="color:blue;">for(var j=0; j<elements[i].length; j++)
            {
                <span style="color:blue;">if(controlName == elements[i][j])
                {
                    callbacks[i](j);
                    return;
                }
            }
        }
    }

    Untested. It's not my job to make sure all 50 cases are identical so I'm assuming they are. I don't claim this to be a good solution. :P

  • Bernie (unregistered)

    Am I the only one that noticed this?

    case "Text42":
    {
        window.event.returnValue=false;
        window.event.cancel = true;
        document.forms[0].elements[6*9].focus();
        break;
    }
    
  • Karen (unregistered) in reply to halcyon1234

    No, it is the other way around. Switch statements are faster because in complier, they are done by jump tables.

    The better way to do this is through REGEX/Pattern matching.

    saves yourself a whole lot code space

  • (cs) in reply to Karen
    Karen:
    The better way to do this is through REGEX/Pattern matching.

    saves yourself a whole lot code space

    Ah, yes, that would be a nice solution.

  • What he said (unregistered) in reply to campkev
    campkev:
    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.

    I AM APPLAUDING YOU SO HARD RIGHT NOW!

  • (cs)

    optimizing the main loop:

    int val;
    if (1==sscanf(name, "text%d", &val)) {
      if (val>0 && val<=50) {
        //do stuff here
      }
    }
    
    

    it's still weird and unfriendly, but at least it's short.

  • Wim Haanstra (unregistered) in reply to attemp1

    How about using a regular expression on the elementItem and then check if the value is between 1 and 50?

    Should at max take 5 lines of code or something. Also easier to manage.

  • JP (unregistered) in reply to halcyon1234

    If you're going to optimize, you may as well go all the way...

    function submitTrap3() { var elementItem; var elements = document.forms[0].elements;

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

    }

  • ShatteredArm (unregistered)

    I know, it should've been dynamic!

    (I don't often use JavaScript, so excuse the possibly incorrect syntax...)

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

    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))) { makeSubmitTrapDelegate(elementItem)(); } } } }

  • oreth lecroix (unregistered) in reply to attemp1

    it can be -assumed- that these are the only available values.

    As such:

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

  • logic (unregistered) in reply to Karen
    Comment held for moderation.
  • JoAnne (unregistered)

    If I'm reading this right, the text being tested is one of the names of the controls on the form. Not text entered by a user.

    If you are the programmer, you've can choose the names of these controls.

    So if there are more text controls, you can name the other controls something that doesn't begin with "Text" and just test for whether the left four characters of the control name is "Text".

    Hence:

    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))) { if (elementItem == "header1:SearchBox") __doPostBack('header1:goSearch',''); else if (left(elementItem,4) == "Text") { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); }

                }
            }
        }
    }
    

    }

    Even if you don't have control over the names, you can test for element names of the form "Text" followed by 1 thru 50, rather than testing each single one, as cparker noted.

  • (cs)

    The single most important thing to consider when coding is extensibility. That being said, this coders solution is pretty good because you can change the handling code for each case on a case by case basis.

    Personally, I would have stored the control text and handler code in an XML configuration file and built the switch statement on the fly using reflection.

  • ShatteredArm (unregistered) in reply to mjk340
    mjk340:
    The single most important thing to consider when coding is extensibility. That being said, this coders solution is pretty good because you can change the handling code for each case on a case by case basis.

    Personally, I would have stored the control text and handler code in an XML configuration file and built the switch statement on the fly using reflection.

    It would be even better if you would expose the handler code via an AJAX web service, and determine which handler to fetch using a rules engine.

  • Ryan Ginstrom (unregistered)

    OK, I've got to say it. TRWTF is that there are 50 freaking text boxes on this page.

  • Gitsnik (unregistered) in reply to halcyon1234
    halcyon1234:
    Oh, that's easy. Switch statements use up extra cycles because of all those extra condition checkers. He should use a single IF statement, since each one is going to focus on n+1 anyways. This way you can get all those switch statements done with just one line. Sure, it'll be a long line, and you'll need to horizontal scroll to read it-- but the horizontal scroll bar only uses pre-compile cycles.
    I see your if statement and raise you a shorter version:

    If(elementItem.replace(/Text/, '') < 50 && elementItem.replace(/Text/, '') > 1) { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); }

    Actually come to think of it I'm not sure that's valid.

  • (cs) in reply to Karen
    Karen:
    No, it is the other way around. Switch statements are faster because in complier, they are done by jump tables.

    The better way to do this is through REGEX/Pattern matching.

    saves yourself a whole lot code space

    Can we have more trolls by girls, please?

    I'm not sure how to do this, but I'd suggest acquiring adverts featuring "Caribbean Boy." Irish Boy doesn't quite have the same appeal, somehow.

  • Marck (unregistered) in reply to attemp1

    I guess there were at least 51 other elementitems...

  • Reuben (unregistered)

    Easiest solution: test to see if it begins with "Text", and if the remaining digits are numeric.

  • Bill (unregistered)

    How about this:

    char strNum[2]; int num;

    if(elementItem == "header1:SearchBox") { __doPostBack('header1:goSearch',''); } else if((strstr(elementItem, "Test")) == elementItem) { strcpy(strNum, &elementItem[4]); num = atoi(strNum); if(num > 0 && (num < 51)) { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); } }

  • Rick (unregistered)

    Looks like that code might have been auto-generated? Like some of those tools that add javascript validation to your webpages automatically.

  • (cs) in reply to JoelKatz
    JoelKatz:
    I DEFY ANYONE TO DEFEND THIS CODE!
    To the TopCav3!
  • Lolzorinos (unregistered)

    Ever heard of DRY - do not repeat yourself. Lol, I hope that guy is fired.

  • (cs) in reply to anon
    anon:
    if (elementItem == "header1:SearchBox") { __doPostBack('header1:goSearch',''); } elseif (("" + elementItem).match(/^Test\d+$/)) { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); }

    o rly?

  • (cs)

    TRWTF is that if this is "submitTrap3", then there must also be submitTrap2 and submitTrap1.

  • anonymous (unregistered) in reply to attemp1

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

    ^Text([1-9]|[1-4][\d]{1}|50)$

  • ???? (unregistered)

    How about for(int i = 1; i < 51; i++){ if(new String("Text" + i){ do something; } }

  • orly (unregistered) in reply to attemp1
    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;
      }
    } 
    

    You only need to define cases that do not fit the default case.

    /curly braces inside of cases hurts my eyes.

  • persto (unregistered) in reply to pink_fairy
    pink_fairy:
    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"!
    Some people, when confronted with a problem, think "I know, I'll restructure the dev team." Now they have two problems.

    No, wait, that can't be right.

    How do you propose "restructuring the dev team?" With a hammer? When you only have a hammer ...

    Your hammer has been stolen and sent to China as scrap steel. Now what do you do?

  • mos (unregistered) in reply to Hans
    Hans:
    Hmm, a challenge... Maybe it was auto-generated? 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 because in the end it is just a dumb program and not a human being who might see the error of his ways...

    In some ways I find that comforting: the notion that my navigation system will never get angry with me for not doing what it says, but instead just tirelessly recomputes newer, better routes... Same as with this program. The predictability has a zen-like quality that induces a trance-like state where the mind is completely free from the shackles of everyday reality, allowing us to soar above the code and wonder at its shape and beauty.

    I would also like to point out the considerable entertainment value the code is providing, although I will admit if you don't think of that as a valid point in the context of professionally-written computer software ;-)

    Somehow there must be a higher plane for the minds of people who produce this as well. And maybe, just maybe the programmer ascended his mind even beyond that level to soar above us mere mortals and to produce WTF's so heavenly on that higher plane, that they clash on the pavement of reality with even more impact and destructive force compared to WTF's from the lower planes of insanity.

  • saw (unregistered) in reply to JoelKatz

    He really, really liked to have programs that are at least 375 lines long.

    Programing OCD, perhaps?

  • Azeroth (unregistered) in reply to fourchan
    fourchan:
    Since every case has three statements to execute, he could make it to always fall through two cases:
    int i=-3;
    ...
    case "Text1": window.event.returnValue=false;          if (!++i) break;
    case "Text2": window.event.cancel = true;              if (!++i) break;
    case "Text3": document.forms[0].elements[n+1].focus(); if (!++i) break;
    case "Text4": window.event.returnValue=false;          if (!++i) break;
    case "Text5": window.event.cancel = true;              if (!++i) break;
    case "Text6": document.forms[0].elements[n+1].focus(); if (!++i) break;
    case "Text7": window.event.returnValue=false;          if (!++i) break;
    case "Text8": window.event.cancel = true;              if (!++i) break;
    case "Text9": document.forms[0].elements[n+1].focus(); if (!++i) break;
    case "Text10": window.event.returnValue=false;          if (!++i) break;
    case "Text11": window.event.cancel = true;              if (!++i) break;
    case "Text12": document.forms[0].elements[n+1].focus(); if (!++i) break;
    (...)

    You, sir, are an evil genius. I hope I will never ever have to work with you.

  • sebbb (unregistered)
    #define CaseBegin (n) case "Text" + #n: {
    #define CaseEnd  }
    
    switch(...)
        CaseBegin(1)
             window.event.returnValue=false;
             window.event.cancel = true;
             document.forms[0].elements[n+1].focus();
             break; 
        CaseEnd
        CaseBegin(2)
             window.event.returnValue=false;
             window.event.cancel = true;
             document.forms[0].elements[n+1].focus();
             break; 
        CaseEnd
        CaseBegin(3)
             window.event.returnValue=false;
             window.event.cancel = true;
             document.forms[0].elements[n+1].focus();
             break; 
        CaseEnd
    
        ...
    
        CaseBegin(50)
             window.event.returnValue=false;
             window.event.cancel = true;
             document.forms[0].elements[n+1].focus();
             break; 
        CaseEnd
    }
    
  • Cheong (unregistered) in reply to attemp1
    attemp1:
    how about this?

    switch (elementItem) { case "header1:SearchBox" : { __doPostBack('header1:goSearch',''); break; } case "Text1": . . . 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 making a prototype event handler for Textbox that will perform the task if this.name.indexOf("Text") == 0 ?

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

« Return to Article