- 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
Once upon a time in a place far far away Programmers were paid by the line. As a result LOCs made it's way into the lexicon of history and software found "Easter Eggs" [Gasp!] and the like.
Could this be a remnant of times gone buy (sic)?
Admin
If you use a binary tree...
1-3 4-6 7-10 11-12 etc etc etc
You'd make at most 5 compares?
Admin
And add a hashtable for the labels...
Admin
This right here. This was as far as I had to read to know this code was going to be bad. When I see sloppiness like this, I know right away that the "programmer" is just clumsily ham-fisting everything in while alt-tabbing back and forth between Google cached expert-exchange.com threads.
Captcha: laoreet. Clearly a reference to Lao-Tse's Tao of Hax0ring.
Admin
what about this?
Admin
This is shorter.
if (elementItem == "header1:SearchBox") { __doPostBack('header1:goSearch',''); } else if (elementItem) { textName = elementItem.substr(0,3); textNum = elementItem.substr(4); if (textName == 'Text' and textNum >= 1 and textNum <= 50) { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; } }
Admin
<quote>I just can't see anything else that would make it smaller, expandable and easier to manage...</quote>
How about a regular expressions? Javascript has them
elementItem.matches(/^Text([1-5][0-9]|[0-9]$/);
Admin
Admin
How about:
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(); }
Admin
close... but I'd just put the case stuff in a separate function. Example
form = document.forms[0]; function doThatThing(form ) { window.event.returnValue=false; window.event.cancel = true; form.elements[n+1].focus(); break;
}
Everything between the lowest and highest variable name should not even be cases since nothing is really changing here.
Admin
This code just screams to be an ifelse. Also I hate seeing switches with no default: case. You could optionally use a regex in the else statement to make sure it matches the "Text"n+ format.
if(elementItem == "header1:SearchBox") { __doPostBack('header1:goSearch',''); } else { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; }
Admin
witch (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
Not even breaking up the string, parsing as an Int and just checking if it's 0 < element < 51 ?
Admin
A switch() statement shouldn't be used at all.
if ( elementItem == header1:SearchBox ) {
} else if ( elementItem.substring(0,3) == 'Text' ) {
}
Admin
Admin
one if in a loop, break out of the loop if the condition is met... -ouch
Admin
if(elementItem=='header:SearchBox') _doPostBack('header1:goSearch',''); else { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); }
Admin
Even the replies are stupid... hack it like this:
if (elementItem.substr(0, 4) == "Text") { var num = parseInt(elementItem.substr(4)); if (num > 0 && num < 51) { window.event.returnValue = false; window.event.cancel = true; } document.forms[0].elements[n+1].focus(); } else if (elementItem == "header1:SearchBox") { __doPostBack('header1:goSearch',''); }
Admin
ROFL!
Admin
Admin
for (int i=1; i++; i < 51) { if (elementItem == "Text"+i) { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); } }
Or am I just being a noob?
Admin
well you could loop though 1-50 and concat them into variables i.e. ${'Text'+str(loopVar)}
Admin
Reducing the line count by two or three might actually be harder than reducing it by about 344.
Admin
Well, it's obvious that you ought to dynamically figure out what Text number it is...
Admin
//c# code sample
if(elementItem=="header1:SearchBox")__doPostBack('header1:goSearch',''); else for(int c=1;c<51;c++)
if(elementItem=="Text"+c) { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); }
Admin
default:
Admin
if (elementItem == "header1:SearchBox") { __doPostBack('header1:goSearch',''); break; } else if (elementItem.match(/^Text\d\d) { window.event.returnValue=false; window.event.cancel = true; document.forms[0].elements[n+1].focus(); break; }
...Please tell me you were joking.
Admin
Well, this is a full 4% (609 bytes compared to 15210) if they used spaces, or 6.65% if they used tabs, of the code and does exactly what it does. Probably faster.
and if
is the name of the selected/submitted field like i suspect it is, this is even better.