Comment On If I've Said It Once, I've Said It Fifty Times

I can think of several ways to improve the code below from Jeff S., or at least to reduce its line count by two or three. [expand full text]
« PrevPage 1 | Page 2 | Page 3 | Page 4 | Page 5Next »

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:01 • by Someone You Know
It's a trap!

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:01 • by Kyre (unregistered)
Obviously, TRWTF is that it isn't forward thinking enough to consider the possibility of 60 results.

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:02 • by JoelKatz (unregistered)
I DEFY ANYONE TO DEFEND THIS CODE!

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:06 • by 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.

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:09 • by jpaull
226982 in reply to 226980
JoelKatz:
I DEFY ANYONE TO DEFEND THIS CODE!


<sarcasm>Don't blame the code!!! Blame the programmer!!!</sarcasm>

I don't wonder so much how programmers who write code like this get jobs... what amazes me is that so many of them have College degrees... therefore I would ACTUALLY blame the professors who passed them in the first place.

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:09 • by bitblit
I suddenly have the urge to go diarrhea in my pants.

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:12 • by Skizz
No, wait, I've got it - all those "Text.." labels have the same code! So, all we need to do is to put the common code into a default section and then add in empty case statements for all the other control names, a bit like:

case "Text51":
break;
case "Text52":
break;
case "Label1":
break;
case "Label2":
break;

and so on. Note how I'm also not putting in those redundant curly braces to cut down the size even more!

Skizz

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:12 • by ParkinT
226985 in reply to 226983
bitblit:
I suddenly have the urge to go diarrhea in my pants.

That might 'run on' about as long as this snippet of code!!

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:15 • by Hans (unregistered)
226986 in reply to 226980
JoelKatz:
I DEFY ANYONE TO DEFEND THIS CODE!


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


Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:18 • by Fuzzypig (unregistered)
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"!

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:20 • by too_many_usernames
226989 in reply to 226986
Hans:
JoelKatz:
I DEFY ANYONE TO DEFEND THIS CODE!


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



Excellent!

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:23 • by robert (unregistered)
It's a trap of course,
anyway I try it: the "n+1" pattern?

(what is a pattern?)

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:23 • by Bombe
I don’t see what’s wrong with that code.

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:25 • by Gustav Dahlkvist (unregistered)
THE GOGGLES DO NOTHING

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:26 • by Lennart (unregistered)
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.

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:27 • by SpamBot (unregistered)
wow ... just ... wow ...

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:33 • by ice (unregistered)
what you wouldn't do for a default block.
Then again maybe he just wanted to check if anyone read the code, a test !

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:33 • by Dirk Diggler (unregistered)
226997 in reply to 226986
Hans:
JoelKatz:
I DEFY ANYONE TO DEFEND THIS CODE!


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


Awesome.

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:33 • by moi (unregistered)
226998 in reply to 226980
JoelKatz:
I DEFY ANYONE TO DEFEND THIS CODE!
Notice that case 'Text19' and 'Text37' are different, so it's not completely moronic code.

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:33 • by Osno (unregistered)
When you think nobody could improve on the for-switch pattern, someone actually comes up with this. Excellent.

Captcha: genitus, the genious of the devs genitals?

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:34 • by Voter (unregistered)
TRWTF is that we just elected a socialist as the next President... Sorry, off topic...

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:34 • by ck (unregistered)
227001 in reply to 226980
JoelKatz:
I DEFY ANYONE TO DEFEND THIS CODE!


paid per line.

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:34 • by Not Dorothy (unregistered)
227002 in reply to 226980
JoelKatz:
I DEFY ANYONE TO DEFEND THIS CODE!


I was on piece work :)

In the days of COBOL this sort of thing was not unknown when programmer productivity was measured in KLOC.

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:35 • by snoofle
I know, I know!!! It needs xml and xslt!!!

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:35 • by Eric (unregistered)
var f =0;


There you go. He declared that variable and never used it. That will cut down at least one line......

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:37 • by snoofle
227005 in reply to 226980
JoelKatz:
I DEFY ANYONE TO DEFEND THIS CODE!
Ok TopCod3r, THIS is one of those places where you should inject one of your classic responses!

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:37 • by Mr B
<DefendingTehCodez>
It might have been automatically produced by some sort of codegen.

Anyone who has ever used Documentum will be familiar with using XSLT and XML content to automatically generate HTML, ASP, ASP.Net, Javascript, you name it...(adding client-side validation based on the contents on the XML source for example would result in hideous code repetition, but not necessarily code that you'd have to maintain manually)...
</DefendingTehCodez>

...but it's more likely to simply be written by one.

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:37 • by DaveAronson
227007 in reply to 226994
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.

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:43 • by marcan (unregistered)
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.

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:44 • by Smash King
OOH I know it! He can cut a few lines by using K & R or BSD KNF indentation styles.

Addendum (2008-11-05 08:53):
And I would like to add to my fellow-countrymen' fun: "É uma cilada, Bino!"

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:53 • by pscs
227014 in reply to 227007
DaveAronson:
That could just be consistent style. There are probably^Wcertainly "developers" out there stupid enough to type that all in by hand.


But it's NOT consistent:

window.event.returnValue=false;
window.event.cancel = true;

(Spaces round the '=' on the second line, but not the first)


Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:55 • by Mii (unregistered)
I see the pattern.. each line has characters !

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:57 • by D0R
Hmmm. A pattern... What could it be? It's like trying to find an elephant in a hay stack!

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 08:58 • by Steenbergh (unregistered)
227018 in reply to 226992
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...

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 09:05 • by spamcourt
227019 in reply to 226980
JoelKatz:
I DEFY ANYONE TO DEFEND THIS CODE!


LEAVE THE CODE ALONE! ;_;

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 09:06 • by 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.


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

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 09:07 • by attemp1 (unregistered)
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...

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 09:07 • by 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;
(...)

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 09:09 • by Me (unregistered)
The real WTF is that the difference between Summary and Full Article on the front page is the final line.

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 09:11 • by Valerion (unregistered)
227024 in reply to 227001
ck:
JoelKatz:
I DEFY ANYONE TO DEFEND THIS CODE!


paid per line.


I thought the exact same thing.

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 09:11 • by RazZziel (unregistered)
227025 in reply to 227022
Would you marry me?

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 09:13 • by KenW
227026 in reply to 226988
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!".

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 09:16 • by Neb (unregistered)
If this code were truly evil one of the cases, say about 2/3 of the way down, would be just a tiny bit different. with no explanation.

Re: If I've Said It Once, I've Said It Fifty Times...

2008-11-05 09:18 • by Skizz
...don't exaggerate.

And it's back

2008-11-05 09:19 • by MAG (unregistered)
Good to see the for-case paradigm back :)

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 09:20 • by cthulhu (unregistered)
227030 in reply to 226980
JoelKatz:
I DEFY ANYONE TO DEFEND THIS CODE!


Well it works. What's the problem?

Time? Well assuming this code supports hours of runtime behavior and the copy pasting took 20 minutes, that's a fair tradeoff.

And what's the worse that can happen in the future?

Well if they need another 100 text fields, well okay that's a lot of copy-paste, perhaps 15 minutes work though. Not a big deal, especially if such a request is very unlikely to happen.

What if they need to edit the implementation for all of the text boxes? It might take 5 minutes to do that with search and replace. Again not a big deal.

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.

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 09:21 • by Welbog
227031 in reply to 227022
fourchan:
Since every case has three statements to execute, he could make it to always fall through two cases:
...
That is simply sinister. Thank you, my good man!

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 09:21 • by anon (unregistered)
227032 in reply to 227021
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();
}

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 09:23 • by WhiteSpace (unregistered)
227033 in reply to 227020
halcyon1234:
Oh, that's easy. Switch statements use up extra cycles because of all those extra condition checkers.


Better read up on your switch statements again there sonny.

Re: If I've Said It Once, I've Said It Fifty Times

2008-11-05 09:30 • by gosse (unregistered)
227034 in reply to 227033
WhiteSpace:
halcyon1234:
Oh, that's easy. Switch statements use up extra cycles because of all those extra condition checkers.


Better read up on your switch statements again there sonny.

Better check up on your sarcasm detector again there sonny.
« PrevPage 1 | Page 2 | Page 3 | Page 4 | Page 5Next »

Add Comment