- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
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(); ... } } }
Admin
Replace > sign above with >= and < with <=.
Admin
It's JavaScript, so:
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.)
Admin
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; } }
Admin
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
Unless there are conditions that are not mentioned here, this should work.
Admin
You might want to think some more. Assuming Text1-50 cases are the same, you actually have three possibilities.
Admin
Sure there is, you can always add new lines!
Admin
you just need 2 case statements u brainfucks! No go figure how, cause I won't tell you, because you won't learn anything.
Admin
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?
Admin
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!
Admin
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.
Admin
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.
Admin
Admin
Yeah, a lot of times it's easier to copy/paste than it is to think.
Admin
I posted this earlier but all my formatting disappeared. Turns out there's a BBCode for that :-)
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
Admin
No, wait, that can't be right.
How do you propose "restructuring the dev team?" With a hammer? When you only have a hammer ...
Admin
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
Admin
Am I the only one that noticed this?
Admin
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
Admin
Admin
I AM APPLAUDING YOU SO HARD RIGHT NOW!
Admin
optimizing the main loop:
it's still weird and unfriendly, but at least it's short.
Admin
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.
Admin
If you're going to optimize, you may as well go all the way...
function submitTrap3() { var elementItem; var elements = document.forms[0].elements;
}
Admin
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)(); } } } }
Admin
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; }
Admin
With compiled code, yes, a 'switch' statement is generally faster (though 'Select Case' in Visual Basic is nothing more than syntactic sugar on top of a If/ElseIf/../End If structure). Languages that support 'switch' on strings usually implement it by either hashing or radix searching, using highly-specialized implementations that vastly outperform manually hashing.
However, this code is Javascript embedded in a web page. I have tested with IE, Firefox and Google Chrome and found that in all cases, a 'switch' block with 50 string 'case' statements performed significantly slower than a single hash lookup. The following table summarizes my findings:
The code I used to measure these is here: http://israel.logiclrd.cx/SwitchVsHash.html
Of course this is all completely meaningless because we're talking about a lookup that takes place in response to the user pressing the Enter key on the keyboard. In all likelihood, it will finish being processed before they have had time to lift their finger back up off the key. :-)
Admin
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.
Admin
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.
Admin
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.
Admin
OK, I've got to say it. TRWTF is that there are 50 freaking text boxes on this page.
Admin
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.
Admin
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.
Admin
I guess there were at least 51 other elementitems...
Admin
Easiest solution: test to see if it begins with "Text", and if the remaining digits are numeric.
Admin
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(); } }
Admin
Looks like that code might have been auto-generated? Like some of those tools that add javascript validation to your webpages automatically.
Admin
Admin
Ever heard of DRY - do not repeat yourself. Lol, I hope that guy is fired.
Admin
o rly?
Admin
TRWTF is that if this is "submitTrap3", then there must also be submitTrap2 and submitTrap1.
Admin
^Text([1-9]|[1-4][\d]{1}|50)$
Admin
How about for(int i = 1; i < 51; i++){ if(new String("Text" + i){ do something; } }
Admin
You only need to define cases that do not fit the default case.
/curly braces inside of cases hurts my eyes.
Admin
Admin
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.
Admin
He really, really liked to have programs that are at least 375 lines long.
Programing OCD, perhaps?
Admin
You, sir, are an evil genius. I hope I will never ever have to work with you.
Admin
Admin