• Steve (unregistered)

    now this one is funny!! :)

    poor guy

  • Zerg (unregistered)

    You poor bastard!!!

  • JWT (unregistered)

    Maybe he used a macro

  • Adrian Florea (unregistered)

    What can I say?... CHO_PLEASE_EXIT :-)

  • Charl Marais (unregistered)

    Hehe, bet this guy gets paid based on the number of lines of code...

  • Phillip Calçado (unregistered)

    > Hehe, bet this guy gets paid based on the
    > number of lines of code...

    "What was the biggest project you worked?"

    "Oh, you know... this 10,000,000 LOC system I deleloped all by miself..."

  • Bruno Bord (unregistered)

    >this was written before Java implemented loops

    LOL ! I hope he used the clipboard... mmm...
    not sure...

  • icelava (unregistered)

    He used a scripting language to loop += 500 to churn out this block of code.

  • Centaur (unregistered)

    You don’t understand, it’s called “loop unrolling” and is generally considered a performance optimization :)

    Just kidding, we all know where premature optimization gets us.

  • Mike R. (unregistered)

    It pains me to know people like this get programming positions.

  • tinae (unregistered)

    I just found this site today. Boy, am I enjoying it. I do reviews on a daily basis (and do a lot of them!) for a dev shop of about 200 developers. What I have seen here gives me hope. It could have been so much worse!

  • Phil Scott (unregistered)

    I think it's quite clear what this developer is trying to accomplish. At some point their Java app was running slow. They fired up their test environment, and get to work on the first thing that pops up on the screen, which is the code to populate a listbox. Well, the time to display this listbox was noticable. So, said developer gets hacking. They quickly throw together a macro to knock this code out, not only unrolling the loop and saving precesious milliseconds checking the loop condition (we are talking about almost 80 iterations here), but also saved over 80 casts from an int to a string.


    Having sat back and admired their work at optimizing the "add items to listbox" code, the developer recompiles and fires up the app. This is when they began to realize that their UI done in java is going to be unresponsive and slow no matter what. But at least it runs on solaris.

  • asdfs (unregistered)

    Isn't there possibly some Java IDE that generates this code in a wysiwig manner? You just add the items (poor guy, adding all this bullshit) manually and the code is generated automatically?

  • :) (unregistered)

    But how can you loop strings? "500" + "500" = "1000"?

    Hehe :)

  • Juan Ortiz (unregistered)

    By doing something like this: (i haven't programmed in Java in a long time so bear with me)
    toString(toInt("500") + toInt("500"))

    All he needed to do was:

    annualMileageChoice = new Choice();
    annualMileageChoice.addItem(CHO_PLEASE_ENTER);
    for(int i = 500; i <= 45000; i += 500)) {
    annualMileageChoice.addItem(toString(i));
    }

  • ben.lovell@bbc.co.uk (Ben) (unregistered)

    Loop unrolling? No. Its not bundling more than one action into an iteration is it? Its not even iterating. :)

  • Jason Barnabe (unregistered)

    I wonder if this page used a server-side loop to display this WTF.

  • Marvin Smit (unregistered)

    Ehhh, who would want to program in a language "not having ANY loop constructions" in the first place?

  • Matthew W. Jackson (unregistered)

    Why isn't this written using recursion?

  • hassan.voyeau@gmail.com (Hassan Voyeau) (unregistered)

    "Why isn't this written using recursion?"

    I am guessing that you are being sarcastic but I would not want to assume this.
    Recursion is more difficult to debug and incurs performance overheads in the creation and destruction of automatic variables and in function calls.

  • Matthew W. Jackson (unregistered)

    Sarcasm? Hardly. Well, yes, but, you're missing my point.

    If this were in fact written in a language without loop structures (and there are such languages), it could still be implemented using recursion. While Java may not be suited for this kind of recursion, it would be perfectly acceptable in a functional programming language.

  • Matthew W. Jackson (unregistered)

    Just had another thought. This solution is still easier to understand than a C++ Template-Based Metaprogramming version, where template specialization lets the compiler basically exapand the whole thing for you.

    But I seriously doubt the coder typed this thing completely by hand anyway. I personally use Excel to write any code that is this repetitive that simply can't be done in a loop (obviously that doesn't apply here). I call it SSMP (Spreadsheet Metaprogramming).

  • Distilled Software Hate (unregistered)

    The important question is... why didn't he use Enterprise Java Beans to make this BETTER!

  • Matthew W. Jackson (unregistered)

    Enterprise Java Beans? Is that something I can get at Starbucks? Is it some promotional tie-in with the Star Trek Franchise?

  • Guido (unregistered)

    Excel? Is that not that strange program people use for just about everything that remotely has anything to do with lists or even tables? I guess I must have that one installed somewhere too... searches around a little ... not found yet, buried too deep under useful programs...

  • cbhacking (unregistered) in reply to Juan Ortiz
    Juan Ortiz:
    By doing something like this: (i haven't programmed in Java in a long time so bear with me) toString(toInt("500") + toInt("500"))

    All he needed to do was:

    annualMileageChoice = new Choice(); annualMileageChoice.addItem(CHO_PLEASE_ENTER); for(int i = 500; i <= 45000; i += 500)) { annualMileageChoice.addItem(toString(i)); }

    Strictly speaking, that doesn't work - Java doesn't have any built-in functions like toString or toInt. (The closest it has are the functions defined in java.lang.Object, which are inherited by all Java classes and are therefore callable from almost anywhere in a Java program. However, the default toString function in Java doesn't take any parameters anyhow - it just prints out a unique identifier for your object.)

    The easiest way to cast an int to a String in Java is simply to use string concatenation (via the + operator) so you'd have the following:

    annualMileageChoice = new Choice(); annualMileageChoice.addItem(CHO_PLEASE_ENTER); for(int i = 500; i <= 45000; i += 500)) { annualMileageChoice.addItem("" + i); }

    However, some people find that a bit hack-ish and prefer the explicit conversion functions:

    // Use a static function of the String class annualMileageChoice.addItem(String.valueOf(i));

    // Use a static toString function found in the Integer class annualMileageChoice.addItem(Integer.toString(i));

    // Use an actual Integer object's toString() function annualMileageChoice.addItem((new Integer(i)).toString());

Leave a comment on “Remember when Java didn't have any loop structures?”

Log In or post as a guest

Replying to comment #:

« Return to Article