• (cs)

    It's a trap!

  • Kyre (unregistered)

    Obviously, TRWTF is that it isn't forward thinking enough to consider the possibility of 60 results.

  • JoelKatz (unregistered)

    I DEFY ANYONE TO DEFEND THIS CODE!

  • (cs)

    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.

  • (cs) in reply to JoelKatz
    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.

  • (cs)

    I suddenly have the urge to go diarrhea in my pants.

  • (cs)

    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

  • (cs) in reply to bitblit
    bitblit:
    I suddenly have the urge to go diarrhea in my pants.
    That might 'run on' about as long as this snippet of code!!
  • Hans (unregistered) in reply to JoelKatz
    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 ;-)

  • 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"!

  • (cs) in reply to Hans
    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!
  • robert (unregistered)

    It's a trap of course, anyway I try it: the "n+1" pattern?

    (what is a pattern?)

  • (cs)

    I don’t see what’s wrong with that code.

  • Gustav Dahlkvist (unregistered)

    THE GOGGLES DO NOTHING

  • 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.

  • SpamBot (unregistered)

    wow ... just ... wow ...

  • 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 !

  • Dirk Diggler (unregistered) in reply to Hans
    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.
  • moi (unregistered) in reply to JoelKatz
    JoelKatz:
    I DEFY ANYONE TO DEFEND THIS CODE!
    Notice that case 'Text19' and 'Text37' are different, so it's not completely moronic code.
  • 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?

  • Voter (unregistered)

    TRWTF is that we just elected a socialist as the next President... Sorry, off topic...

  • ck (unregistered) in reply to JoelKatz
    JoelKatz:
    I DEFY ANYONE TO DEFEND THIS CODE!

    paid per line.

  • Not Dorothy (unregistered) in reply to JoelKatz
    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.

  • (cs)

    I know, I know!!! It needs xml and xslt!!!

  • Eric (unregistered)

    var f =0;

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

  • (cs) in reply to JoelKatz
    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!
  • (cs)
    <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.

  • (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.
    That could just be consistent style. There are probably^Wcertainly "developers" out there stupid enough to type that all in by hand.
  • 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.

  • (cs)

    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!"

  • (cs) in reply to DaveAronson
    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)

  • Mii (unregistered)

    I see the pattern.. each line has characters !

  • (cs)

    Hmmm. A pattern... What could it be? It's like trying to find an elephant in a hay stack!

  • Steenbergh (unregistered) in reply to Bombe
    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...

  • (cs) in reply to JoelKatz
    JoelKatz:
    I DEFY ANYONE TO DEFEND THIS CODE!

    LEAVE THE CODE ALONE! ;_;

  • (cs)

    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(); }
  • 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...

  • (cs)

    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;
    (...)
  • Me (unregistered)

    The real WTF is that the difference between Summary and Full Article on the front page is the final line.

  • Valerion (unregistered) in reply to ck
    ck:
    JoelKatz:
    I DEFY ANYONE TO DEFEND THIS CODE!

    paid per line.

    I thought the exact same thing.

  • RazZziel (unregistered) in reply to fourchan

    Would you marry me?

  • (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"!

    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!".

  • 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.

  • (cs)

    ...don't exaggerate.

  • MAG (unregistered)

    Good to see the for-case paradigm back :)

  • cthulhu (unregistered) in reply to JoelKatz
    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.

  • (cs) in reply to fourchan
    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!
  • anon (unregistered) in reply to attemp1

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

  • WhiteSpace (unregistered) in reply to halcyon1234
    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.

  • gosse (unregistered) in reply to WhiteSpace
    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.

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

« Return to Article