• -L (unregistered)
    Alex Papadimoulis:

    caseMgr.setAssetId(result.getResults().elementAt(0).toString());
    caseMgr.setTypName(result.getResults().elementAt(1).toString());
    caseMgr.setValCode(result.getResults().elementAt(2).toString());
    caseMgr.setValDesc(result.getResults().elementAt(Integer.parseInt("3")).toString());

    Is there a 10 character limit to the method names in Java?

    As for the rest, I cannot think of any excuses doing it this way. A real WTF.
  • (cs) in reply to -L

    Maybe Integer.parseInt("3").toString() isn't the same as "3" in his implementation of Java.

  • (cs)

    Now, wait a second, haven't you heard of Trinary Java? To use decimal, he would have had to prepend each decimal number with 0d, and that would have looked silly. Lay off of him.

  • Canuck (unregistered)

    This is probably one of those situations where the client complained that the original code was too efficient, so the developper went back and tried to slow it down.  I think I spoke to him, and he was saying that if the client still wasn't satisified, is next step was going to be something like:

    run(aPhoneNumber, "substring", new Object[]{new Integer(Integer.parseInt("3"))}, new Integer(Integer.parseInt("3"))], new Class[]{int.class, int.class});

    ...

  • (cs) in reply to Canuck

    <FONT face=Georgia>Smart guy to use 3 as the cutoff because if he went to 4, then he would have to hardcode Pi</FONT>

    <FONT face=Georgia>(This post is [image] approved)</FONT>

  • frosty (unregistered) in reply to travisowens

    It's like the stories I hear that go like this:

    A non-software engineers will write a 500 line function.  Later, he is told the function is not necessary.  His response is to spend another 40 lines undoing what the previous 500 lines did.

  • (cs)

    I was going to post a real comment about this, but I just can't stop laughing: But numbers three and above ... hard-coding those was just a flat-out bad practice. Everyone knows that you should use strings for those ...

  • Lews (unregistered) in reply to rogthefrog
    rogthefrog:
    Maybe Integer.parseInt("3").toString() isn't the same as "3" in his implementation of Java.

    Double check the parenthesis again. He's calling toString() on elementAt(Integer.parseInt("3"))

  • nekomimi (unregistered) in reply to travisowens

    Object-oriented programming at its finest.

    No, really.

  • lalala (unregistered)

    This happens when you have to follow brain dead coding standards and design rules. I bet that poor guy had been hit over his head with a 200 page style guide, because he once hard coded a number greater 3. This was his little revenge: Code in full compliance with the rules, so no quality clown could reject the code.

  • (cs) in reply to lalala
    Anonymous:
    This happens when you have to follow brain dead coding standards and design rules. I bet that poor guy had been hit over his head with a 200 page style guide, because he once hard coded a number greater 3. This was his little revenge: Code in full compliance with the rules, so no quality clown could reject the code.


    I think (hope) that this is what actually happened... 
  • Howard M. Lewis Ship (unregistered)

    He was probably on a project that used Checkstyle to look for magic numbers and used parseInt() as a stupid end-run around the Checkstyle rules.  He should have defined int constants for what each element meant (ASSET_ID_INDEX = 0, TYPE_NAME_INDEX = 1), etc.

  • frosty (unregistered) in reply to Howard M. Lewis Ship
    Anonymous:
    He should have defined int constants for what each element meant (ASSET_ID_INDEX = 0, TYPE_NAME_INDEX = 1), etc.


    This is one coding practice often see ignored.  It's a bother if it's only ints that aren't defined constants, but it's a mess if it's strings that show up in 20 places in code.
  • Anonymous (unregistered)

    If I close my eyes and count to Integer.parseInt("10")).toString(), maybe it will go away.  ;^)

  • (cs) in reply to Howard M. Lewis Ship
    Anonymous:
    He was probably on a project that used Checkstyle to look for magic numbers and used parseInt() as a stupid end-run around the Checkstyle rules.  He should have defined int constants for what each element meant (ASSET_ID_INDEX = 0, TYPE_NAME_INDEX = 1), etc.

    Or even constants like ZERO, ONE, TWO, THREE, FOUR, etc.  For full marks, make sure that ZERO is not actually 0...
  • cdoom (unregistered)
    What, no try/catch block around the parseInt call!
  • Eduardo Habkost (unregistered) in reply to lalala

    I guess we've found the author of the code. 8)

    Really, in an environment like "lalala" described, I would do the same.

  • (cs) in reply to Howard M. Lewis Ship
    Anonymous:
    He was probably on a project that used Checkstyle to look for magic numbers and used parseInt() as a stupid end-run around the Checkstyle rules.  He should have defined int constants for what each element meant (ASSET_ID_INDEX = 0, TYPE_NAME_INDEX = 1), etc.


    Maybe he could have used (1+1+1) for 3.....
  • Eduardo Habkost (unregistered) in reply to Eduardo Habkost
    Anonymous:
    I guess we've found the author of the code. 8)

    Really, in an environment like "lalala" described, I would do the same.

    Ugh. Just in case: this was supposed to be a reply to "lalala" comment.

  • (cs) in reply to frosty

    Anonymous:
    It's like the stories I hear that go like this:

    A non-software engineers will write a 500 line function.  Later, he is told the function is not necessary.  His response is to spend another 40 lines undoing what the previous 500 lines did.

    <FONT face="Courier New" size=2>ain't that the truth.  *zing*</FONT>

  • (cs) in reply to Grimoire

    Grimoire:
    ZERO is not actually 0...

    <FONT face="Courier New" size=2>for full kroner, #define Z 0</FONT>

  • (cs) in reply to rogthefrog
    rogthefrog:
    Maybe Integer.parseInt("3").toString() isn't the same as "3" in his implementation of Java.


    Actually, it isn't the same in ANY spec-conforming Java implementation. The former will create a different object each time it is executed, while the latter is taken from the class's constant pool. Or, in code:

    "3" == "3"
    is true, while
    "3" == Integer.parseInt("3").toString()
    is false.

    Of course, it hardly ever makes sense to deliberately create non-identical strings with the same content, and if you really have to, that's what the String(String) constructor is for.
  • (cs) in reply to rogthefrog
    rogthefrog:
    Maybe Integer.parseInt("3").toString() isn't the same as "3" in his implementation of Java.


    Well, of course.  He is trying to account for localization.
  • (cs) in reply to emptyset
    emptyset:

    Grimoire:
    ZERO is not actually 0...

    <font face="Courier New" size="2">for full kroner, #define Z 0</font>

    Nope, #define Z 7

  • (cs) in reply to brazzy
    brazzy:
    rogthefrog:
    Maybe Integer.parseInt("3").toString() isn't the same as "3" in his implementation of Java.


    Actually, it isn't the same in ANY spec-conforming Java implementation. The former will create a different object each time it is executed, while the latter is taken from the class's constant pool. Or, in code:

    "3" == "3"
    is true, while
    "3" == Integer.parseInt("3").toString()
    is false.

    Of course, it hardly ever makes sense to deliberately create non-identical strings with the same content, and if you really have to, that's what the String(String) constructor is for.


    I think when he said "isn't the same", he meant it like most people would take it, that it represents the same set of characters (i.e. .equals()), which would be true.
    "3".equals("3")    is true
    "3".equals(Integer.parseInt("3").toString())   is also true
  • (cs) in reply to brazzy
    brazzy:
    rogthefrog:
    Maybe Integer.parseInt("3").toString() isn't the same as "3" in his implementation of Java.


    Actually, it isn't the same in ANY spec-conforming Java implementation. The former will create a different object each time it is executed, while the latter is taken from the class's constant pool. Or, in code:

    "3" == "3"
    is true, while
    "3" == Integer.parseInt("3").toString()
    is false.

    Of course, it hardly ever makes sense to deliberately create non-identical strings with the same content, and if you really have to, that's what the String(String) constructor is for.


    However...

    "3" == Integer.parseInt("3").toString().intern()

    is true

  • (cs) in reply to kipthegreat
    kipthegreat:
    brazzy:
    rogthefrog:
    Maybe Integer.parseInt("3").toString() isn't the same as "3" in his implementation of Java.


    Actually, it isn't the same in ANY spec-conforming Java implementation. The former will create a different object each time it is executed, while the latter is taken from the class's constant pool. Or, in code:

    "3" == "3"
    is true, while
    "3" == Integer.parseInt("3").toString()
    is false.

    Of course, it hardly ever makes sense to deliberately create non-identical strings with the same content, and if you really have to, that's what the String(String) constructor is for.


    I think when he said "isn't the same", he meant it like most people would take it, that it represents the same set of characters (i.e. .equals()), which would be true.
    "3".equals("3")    is true
    "3".equals(Integer.parseInt("3").toString())   is also true


    Of course it should be noted that the WTF code never actually did   Integer.parseInt("3").toString()
  • (cs) in reply to kipthegreat
    kipthegreat:
    kipthegreat:
    brazzy:
    rogthefrog:
    Maybe Integer.parseInt("3").toString() isn't the same as "3" in his implementation of Java.


    Actually, it isn't the same in ANY spec-conforming Java implementation. The former will create a different object each time it is executed, while the latter is taken from the class's constant pool. Or, in code:

    "3" == "3"
    is true, while
    "3" == Integer.parseInt("3").toString()
    is false.

    Of course, it hardly ever makes sense to deliberately create non-identical strings with the same content, and if you really have to, that's what the String(String) constructor is for.


    I think when he said "isn't the same", he meant it like most people would take it, that it represents the same set of characters (i.e. .equals()), which would be true.
    "3".equals("3")    is true
    "3".equals(Integer.parseInt("3").toString())   is also true


    Of course it should be noted that the WTF code never actually did   Integer.parseInt("3").toString()


    Now that I think about it,   Integer.parseInt("3").toString()   would never compile...  Integer.parseInt("3") is an int, which is primitive, so .toString() couldn't compile....
  • (cs) in reply to Lews
    Anonymous:
    rogthefrog:
    Maybe Integer.parseInt("3").toString() isn't the same as "3" in his implementation of Java.

    Double check the parenthesis again. He's calling toString() on elementAt(Integer.parseInt("3"))



    Oh right, my mistake. It all makes sense now.

     * rogthefrog goes off to change all his code to reflect this superior design pattern

  • My Name (unregistered)

    Why don't you just make ten louder and make ten be the top number and make that a little louder?

  • (cs) in reply to My Name
    Anonymous:
    Why don't you just make ten louder and make ten be the top number and make that a little louder?


    is that a spinal tap reference?  (i've never seen it...)
  • Xaprb (unregistered) in reply to My Name

    But these go to eleven!

    Seriously, I suspect what's really happened here is someone did a find/replace for some stuff and hit "Replace All" without meaning to.  I have personally done that.  I accidentally did s/""/String.Empty/g in some .NET code once and never found the ensuing havoc until much later.

  • NonDev (unregistered) in reply to Xaprb

    Anonymous:
    But these go to eleven!

    I accidentally did s/""/String.Empty/g in some .NET code once and never found the ensuing havoc until much later.

    Wow! Coding through search&replace - gotta love that :)

    NonDev

  • (cs) in reply to Anonymous

    Anonymous:
    If I close my eyes and count to Integer.parseInt("10")).toString(), maybe it will go away.  ;^)

    But keep in mind that counting to Integer.parseInt("010") is not the same as counting to 010. [H]

  • (cs) in reply to OneFactor

    for (int i = 0; isTrue(i < Integer.parseInt("3")); i++) {
       switch i:
          case 0: System.out.print("W"); break;
          case 1: System.out.print("T"); break;
          case 2: System.out.print("F"); break;
    }

  • Tony Morris (unregistered) in reply to brazzy

    This is not true. The J2SE API Specification does not guarantee that an object is created for this method, nor does it guarantee that an object is not created. The argument that is passed to the method is taken from the constant pool (see VM Spec 4) and an "object is created" (it is indeed created - JLS 3.10.5 iirc) in the same way that one is created for all other String literals - simply, both code samples create an object at class load time (the class containing that constant pool), not one or the other. You may have been confusing that Integer.parseInt returns "something else" (an int) that is not the instance representing the passed String literal itself.

    Here is a related topic to the one I believe was attempting to be highlighted.
    http://jqa.tmorris.net/GetQAndA.action?qids=68&showAnswers=true

    Oh, and if you want to argue, I implement the spec. as my day job :) but I'm always up for a constructive debate.

  • Tony Morris (unregistered) in reply to Tony Morris

    I thought I pressed the "quote" button.
    I was referring to:


    Actually, it isn't the same in ANY spec-conforming Java implementation. The former will create a different object each time it is executed, while the latter is taken from the class's constant pool. Or, in code:

    "3" == "3"
    is true, while
    "3" == Integer.parseInt("3").toString()
    is false.

    Of course, it hardly ever makes sense to deliberately create non-identical strings with the same content, and if you really have to, that's what the String(String) constructor is for.

  • David P. Murphy (unregistered) in reply to Tony Morris
    Anonymous:
    This is not true. The J2SE API Specification does not guarantee that an object is created for this method, nor does it guarantee that an object is not created. The argument that is passed to the method is taken from the constant pool (see VM Spec 4) and an "object is created" (it is indeed created - JLS 3.10.5 iirc) in the same way that one is created for all other String literals - simply, both code samples create an object at class load time (the class containing that constant pool), not one or the other. You may have been confusing that Integer.parseInt returns "something else" (an int) that is not the instance representing the passed String literal itself.

    Here is a related topic to the one I believe was attempting to be highlighted.
    http://jqa.tmorris.net/GetQAndA.action?qids=68&showAnswers=true

    Oh, and if you want to argue, I implement the spec. as my day job :) but I'm always up for a constructive debate.


    I have nothing constructive to say.  I simply want to note that this proves that it is one fscked-up language.

    ok
    dpm
  • Knuta (unregistered) in reply to kipthegreat
    kipthegreat:

    Now that I think about it,   Integer.parseInt("3").toString()   would never compile...  Integer.parseInt("3") is an int, which is primitive, so .toString() couldn't compile....


    Yes, this should clearly be solved with a
    (new Integer(Integer.parseInt("3")).toString() ;)
  • SchoolhouseRock (unregistered)

    I just think this programmer grew up listening to Schoolhouse Rock.  Because everyone who listened to that knows that "3 is a magic number."

  • (cs) in reply to OneFactor
    OneFactor:

    for (int i = 0; isTrue(i < Integer.parseInt("3")); i++) {
       switch i:
          case 0: System.out.print("W"); break;
          case 1: System.out.print("T"); break;
          case 2: System.out.print("F"); break;
    }

    You will never understand.

    int errors = 0; for (int i = 0; isTrue(i < Integer.parseInt("3")); i++) { switch(i) { default: if(i == 0) { System.out.print("W"); } else if(i == 1) { System.out.print("T"); } else if(i == 2) { System.out.print("F"); } else { errors += 1; } } } if(isTrue(errors == Integer.parseInt("12")) { throw new SwitchCaseFailure("Invalid arguments."); }

  • (cs)

    "Magic numbers"? This example looks a bit too artificial.

    Regarding hard-coded values, what I do is just hard-code them in when they aren't likely to be changed and use defines when they are. Converting a string to an int? I hardly see the advantages of this scheme.

    Style guidelines don't help much if taken to the extreme.

  • (cs)

    Not to be funny or anything Alex, but that is the lamest WTF I've seen on this site. Running out of WTF's???

  • (cs) in reply to ray

    RAY, YOU ARE GAY

  • (cs) in reply to petvirus

    BLOW ME!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  • (cs) in reply to ray

    Come on. The poor code being written by 12 years old kids is not fun any more. It's just the same kind of crap any inexperienced programmer writes in his first learning year. :D

  • (cs) in reply to NineSisters

    Agreed!

  • (cs) in reply to kipthegreat

    Thats why in java we use .equals() to compare the content of the object. ;)

  • (cs) in reply to brazzy
    brazzy:
    rogthefrog:
    Maybe Integer.parseInt("3").toString() isn't the same as "3" in his implementation of Java.


    Actually, it isn't the same in ANY spec-conforming Java implementation. The former will create a different object each time it is executed, while the latter is taken from the class's constant pool. Or, in code:

    "3" == "3"
    is true, while
    "3" == Integer.parseInt("3").toString()
    is false.

    Of course, it hardly ever makes sense to deliberately create non-identical strings with the same content, and if you really have to, that's what the String(String) constructor is for.


    Forgot the quote button. :)

    Anyway as I said, this is the reason we have the .equals() funtion in java, as it allows us to compare the contents of an object as opposed to "= =" that will compare the objects themselves to see if they are in fact the same object.
  • (cs) in reply to llxx
    llxx:

    "Magic numbers"? This example looks a bit too artificial.

    Regarding hard-coded values, what I do is just hard-code them in when they aren't likely to be changed and use defines when they are. Converting a string to an int? I hardly see the advantages of this scheme.

    Style guidelines don't help much if taken to the extreme.



    While I cannot confirm this particular example, I'm know programmers who have comparable habits. For example, they say "global variables are bad" and the build incredible complex constructs to imitate the semantics of global variables (but of course it's not better at all, since it is the semantic that makes global variables bad)

Leave a comment on “Magic Number Superstition”

Log In or post as a guest

Replying to comment #:

« Return to Article