• Andrew (unregistered) in reply to MeMyselfandI

    Spot on!

    But that is the dark little secret of this site, isn't it? We all think we are hot programmers, and spitting at someone else's code sure reassures us. Actually we generally post according to different schemes, but they all follow the same patterns

    1.   Write some code yourself, which does the same as the wtf, but oh so much more elegantly
    2.   Use the "Well come on that's not so terrilble guys" (trying to go fo the experience edge)
    3.   Rewrite a worse version of the code, to show that you have understood what the problem was.

    Actually, I find some of the answers really funny, especially for the no 2 posters. For instance "What's the big deal about string = "null" ", or "It's not really bad using the same variable for many purposes, that way you don't have to scroll so much".

    Andrew

  • (cs) in reply to Randolpho
    Anonymous:
    Anonymous:
    Alex Papadimoulis:

    someString = anotherString.ToString().ToString();

     

    Oh, and regarding this... perhaps the second ToString() is in case the first one doesn't work?  Maybe triple redundancy is needed here?

     

    The real WTF is why he didn't do this:

    someString = ((string)string.Copy(anotherString.ToString().Clone().ToString()).ToString()).ToString();
    


    As in the (now famous) usenet newsgroup: alt.wesley.crusher.die.die.die?
  • Me (unregistered) in reply to JoeyLemur

    Because:

    void someFunction(Object obj){

    System.out.println( obj != null ? obj.toString() : "");

    } Useful if you know that obj is something which either IS a String, or returns some human readable value.

    Also, if you are given a string from an Object it is far faster to call toString on it to get the value back than it is to cast it.

    Object obj = "Some String, but it was passed to me as an object";

    String str = obj.toString(); // faster String str = (String) obj; // slower

  • (cs) in reply to Me
    Anonymous:
    Because:

    void someFunction(Object obj){

    System.out.println( obj != null ? obj.toString() : "");

    } Useful if you know that obj is something which either IS a String, or returns some human readable value.

    Also, if you are given a string from an Object it is far faster to call toString on it to get the value back than it is to cast it.

    Object obj = "Some String, but it was passed to me as an object";

    String str = obj.toString(); // faster String str = (String) obj; // slower

    Me thinks like would still be far easier for java devs if null was the instance of a full fledged object, Ruby or Python style, no more NPE...

  • (cs) in reply to SerajewelKS

    Actually, I can think of one good reason why you might want to clone a String in Java. I don't know whether this is true or not, but I've heard rumours that methods like "substring" don't create copies of the actual underlying character array, but simply create new objects which refer to the original String, using the appropriate mathematical magic.

    Now, imagine that you have a three million character long string, and you take a twenty character substring of it. The substring contains a reference to the original string, keeping 6MB (2 bytes per character) around. Clone the substring, and the garbage collector can reclaim the 6MB leaving only the 40 bytes for the substring allocated.

    I guess this might be a contrivedly silly example (three million characters?) but the idea holds.

    Then there's always "intern", for doing exactly the opposite (getting rid of multiple objects with the same content), which ought also to save memory if used properly.

  • (cs) in reply to Hawk777
    Hawk777:
    Actually, I can think of one good reason why you might want to clone a String in Java. I don't know whether this is true or not, but I've heard rumours that methods like "substring" don't create copies of the actual underlying character array, but simply create new objects which refer to the original String, using the appropriate mathematical magic.

    Now, imagine that you have a three million character long string, and you take a twenty character substring of it. The substring contains a reference to the original string, keeping 6MB (2 bytes per character) around. Clone the substring, and the garbage collector can reclaim the 6MB leaving only the 40 bytes for the substring allocated.


    No rumor, this is absolutely correct. You get the source code of java.lang.String with the JDK, take a look.

    Ususally this saves time and space, but in extreme cases like the one you describe you would want to get rid of the unused part. I wouldn't call that "clone" because that implies to me an exact copy of the internal state (that's what Object.clone() does). What you want to use is the String(String) constructor.

  • (cs) in reply to Volmarias
    Volmarias:
    ... Guess what ... at least in Java. ...

    However, since there doesn't appear to be a String.Copy method, I have the sinking suspicion that he created a string class, which extends String, just for the sake of lazy typing.


    It's bad enough that so many people seem to think this code is Java when it is plainly C#.  But you ought to know that even if it were Java, he could not have created a class that extends String, which is a final class.  (I believe the C# equivalent is "sealed".)

  • (cs) in reply to ChiefCrazyTalk
    Anonymous:
    Alex Papadimoulis:

    someString = anotherString.ToString().ToString();



    Oh, and regarding this... perhaps the second ToString() is in case the first one doesn't work?  Maybe triple redundancy is needed here?



    I actually think I know how this one came about.  I think this code monkey thrives on Intellisense(TM), and after he typed "someString = anotherString" he didn't realize that was all he needed, so he hit the period key.

    Intellisense pops up, and he starts scrolling through, looking for a likely-sounding method to call (which is how he came across Copy() in one of the other examples).  He spots "ToString()" and thinks to himself, "Ook!  Ook!" (translation: that looks good), so he hits Enter.

    Just one problem for our pet code grinder.  His other finger was still hovering over the period key, and he accidentally taps it a second time.  Intellisense pops up again, and now he's confused.  "Didn't I just do this?"  But, like the mindless Sphex wasp he is (Google "sphexish"), he repeats the process.  ("Stimulus, response, stimulus, response.  Don't you ever think!?")

    He scrolls down to "ToString" again and hits Enter.  This time he types a semicolon to end the line, and when he tests his masterpiece IT SEEMS TO WORK!  And that is the one true sign for the code grinder.  Any code that seems to work is golden, no matter how it is formatted or how many WTFs it contains.

    I'm sure it's been cited here before, but Charles Petzold has an article in which, among other things, he has some criticisms of Intellisense.

    http://www.charlespetzold.com/etc/DoesVisualStudioRotTheMind.html

  • (cs) in reply to Me
    Anonymous:
    Also, if you are given a string from an Object it is far faster to call toString on it to get the value back than it is to cast it.

    String str = obj.toString(); // faster
    String str = (String) obj; // slower


    (Okay, okay, I KNOW I always triple post when I come here -- sorry -- but, damnit, I only make it here on the weekend and I always come in at the end of an interesting thread.)

    Actually, it seems to depend on the JVM.  I tried it with the "client" JVM in Java 1.5, and the cast was between 2 and 3 times faster.  I got the same results with Java 1.6 build 71.  Then I tried the "server" JVM in Java 1.5, and the call to toString was faster, but they were very close (and they were both a lot faster, overall).  The "server" JVM isn't distributed with the builds of mustang, so I couldn't try that.  My benchmark did "warm up" the code to make sure the methods I was timing had been jitted.

    Of course, I'm sure the cast would be massively slower in the case where it actually had to throw a ClassCastException.

    But this kind of microbenchmark of one language feature is not really such a good idea.  You should be writing whichever is clearer in context, not writing the cast as a call just because it might be faster.  If you profile it you will also see that calling a method on an interface is slower than calling it on the actual class.  Does that mean you should declare your variables and parameters as "ArrayList" instead of "List" for a speed boost?

    It would be a rare method that actually DID something with a list where the speed of dispatching methods on that list was the limiting factor, or the method was so performance-critical that it deserved that degree of attention.  Ditto for casting vs. calling toString.  If you really need to worry about it, then you do, of course, but until then keep in mind the two golden rules of optimization:

    1. Don't do it.
    2. (for experts only) Don't do it yet.

  • (cs) in reply to ParkinT

    "...figured that the best consultant probably charged the second highest rate..."

    There's the WTF. What kind of hiring criteria is THAT? If a company is going to make a hiring choice based on such ridiculous guidelines, they deserve what they get.

    Perhaps the rate for the crummy consultants are high because they are getting less and less business because word is getting around! The good consultants haven't had to raise their rates because their quality is high and they are getting all the work these dingdongs used to get.

    Ow. my head hurts now.

  • szr (unregistered) in reply to E
    E:
    I think everyone needs to add another level of WTF to this: someString = new String(((string)string.Copy(anotherString.ToString().Clone().ToString()).ToString()).ToString().ToCharArray()); Enjoy :)
    I think the biggest WTF is that a type called String that has a ToString() method....

Leave a comment on “The Replacement”

Log In or post as a guest

Replying to comment #:

« Return to Article