- 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
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?
Admin
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; } } } } }}
Admin
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.
Admin
The problem, of course, is useless variable names. "Text50"? psshh... More like "NameOfSecondCousinsFirstDeceasedMotherInLaw".
Admin
TRWTF is that the article preview on the front page, takes up more space than most complete articles.
Admin
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.
Admin
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.
Admin
Hmmm...
Admin
No shit, sherlock.
Admin
Admin
OH God! My Eyes! My Poor Useless Eyes!!
~Sticky
Admin
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.
Admin
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.
Admin
This is pathetic. Submitting fake src. This src cannot be real..
Admin
Admin
Better check up on you smoke detector again there sonny.
.... Seriously, we just finished day light savings, its a great time to do it.
Admin
// 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;
} }
Admin
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.
Admin
Well, we wouldn't want to have the code fire for the 50 corresponding dropdown boxes and 50 option groups, dont'cha know.
Admin
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.
Admin
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.
Admin
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...
Admin
Admin
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.
Admin
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; }
Admin
You know, you're so right. Such overkill!
I should be reprimanded for such waste!
I think it took a minute and a half, including testing. I know, such a waste of time...
Admin
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.
Admin
Admin
I wouldn't last long at this company. My fix would involve a 343 lines-of-code pay cut!
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!!"
Admin
I guess the following is ok, where "offset" is the index such as:
But I suppose the HTML page is also a WTF.
Admin
(v1.1)
Admin
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(); } } } } }
Admin
as already mentioned.....
switch (elementItem) { case "header1:SearchBox": { __doPostBack('header1:goSearch',''); } break;
Admin
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, ...
Admin
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; } }
Admin
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.
Admin
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;
} }
Admin
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.
Admin
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.
Admin
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(); }
Admin
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.
Admin
Shoot. That's easy. Paula wrote it.
Admin
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!
Admin
I'm sorry, apparently i should have added <sarcasm> tags. I'd didn't think people would actually take that seriously.
Admin
Admin
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!
Admin
/sigh
I guess i REALLY need to work on my sarcasm.
Admin
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 :)
Admin
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(); }
Admin
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