String.valueOf()



  • As reported by a colleague, who is doing maintenance work on a legacy application:

    Here's the code used to concatenate three strings:

    String.valueOf( String.valueOf ( new StringBuffer(bla).append(bla2).append(bla3) ) )

    I could almost (almost) accept that if it were the result of a (slighty stupid) visual IDE or code generation tool or whatever. Sadly, it appears this code has been hand-typed by some "brillant" and obviously nowhere-to-be-found developer.



  • Sounds like he IS a code-generating tool. Emphasis on the "tool."



  • @stnever said:

    As reported by a colleague, who is doing maintenance work on a legacy application:

    Here's the code used to concatenate three strings:

    String.valueOf( String.valueOf ( new StringBuffer(bla).append(bla2).append(bla3) ) )

    I could almost (almost) accept that if it were the result of a (slighty stupid) visual IDE or code generation tool or whatever. Sadly, it appears this code has been hand-typed by some "brillant" and obviously nowhere-to-be-found developer.


    Is this Java? Why is String.valueOf() overloaded to take a String as an argument? That's the WTF right there, isn't it?



  • It takes a great deal of effort to restrain myself from falling into my
    "premature optimization" rant, which so thoroughly and resolutely
    applies to people who don't trust Java's ability to make Strings. 
    I suspect it comes from relying on some book that contains ten-year-old
    Java "wisdom."




  • @savar said:


    Is this Java? Why is String.valueOf() overloaded to take a String as an argument? That's the WTF right there, isn't it?


    It is not overloaded.  The String.valueOf method takes an Object.



  • @VGR said:

    @savar said:

    Is this Java? Why is String.valueOf() overloaded to take a String as an argument? That's the WTF right there, isn't it?

    It is not overloaded.  The String.valueOf method takes an Object.

    And even if it hadn't been I would have expected that it makes an independent copy of an argument.

    ...and also what it's all about this code-generators blaming? Aren't they supposed to generate code? Loads of code?



  • @VGR said:

    @savar said:

    Is this Java? Why is String.valueOf() overloaded to take a String as an argument? That's the WTF right there, isn't it?


    It is not overloaded.  The String.valueOf method takes an Object.


    Doh! ...thanks for teaching me.

    http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#valueOf(java.lang.Object)



  • @VGR said:

    @savar said:

    Is this Java? Why is String.valueOf() overloaded to take a String as an argument? That's the WTF right there, isn't it?


    It is not overloaded.  The String.valueOf method takes an Object.

    Well, using it so much will certainly overload it.



  • Creating a new String is what "new String(String)" is for... there are some corner cases to use it; but "String.valueOf(Object)" does (if
    object is not null) "object.toString()" -- which in the String
    implementation just returns itself, so no creation or casting happens.

    Yeah, code generators generate code to be used with Lines of Code counters.... its good for metrics and improves developer efficiency at the same time :) (jk).

    And speaking of needless object creation... I've seen this in some of the code where I work:
    String firstName = new String();
    firstName = getFirstName();
    String lastName = new String();
    firstName = getFirstName();
    ...
    repeat about 20 times



  • I think they forgot something:

    String.valueOf( String.valueOf ( new StringBuffer(bla).append(bla2).append(bla3) .toString()).toString() ).toString()

     

    @beltorak said:

    .....

    And speaking of needless object creation... I've seen this in some of the code where I work:
    String firstName = new String();
    firstName = getFirstName();
    String lastName = new String();
    firstName = getFirstName();
    ...
    repeat about 20 times

    I worked with someone who used to parse Strings to ints like so:

    <FONT face="Courier New" size=2>int a = new Integer(aStr).intValue();</FONT>

    and convert them to Strings like this

    <FONT face="Courier New" size=2>String aStr = new Integer(a).toString();</FONT>

    and when I told him why this was bad practice, his argument was along the lines of using objects is more OO, whereas static methods aren't OO.

    Never could get around the 'logic' of that. At least not with a guy whose first language was VB.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.