• LCrawford (unregistered)

    At least it implements an if (Whitespace) then SetNull pattern automatically. That's the frist thing everyone wants to write anyway.

  • P (unregistered)

    Oh boy, this mistake (mishandling String references and creating tons of String objects as a result) is covered even in OCJP material and exam questions. That should tell you something.

  • (nodebb)

    Somebody needs to challenge him to actually write code to modify a string instance. Maybe he'll succeed, I know in C# you can maybe do something with reflection and unsafe points. But even if he does, he'll realise just how unlikely and illogical it is to protect against it.

  • Naomi (unregistered) in reply to Mr. TA

    Yup, you can do the same trick in Java - grab the Field holding the backing char[] suddenly you can mutate strings.

    But why stop there? The runtime maintains a cache of Integer instances for autoboxing (the process that converts a primitive int into an object), IIRC on the range of a signed byte. If you want to be really nasty you can reflectively replace one of those Integers and suddenly 7 != (int) (Integer) 7.

    There's a reason languages with reflection caution against using it willy-nilly, I suppose.

  • D (unregistered) in reply to Mr. TA

    Yes, you can modify a string in Java with some (ab)use of reflection (at least in OpenJDK). What's funny is that new String(otherString) doesn't even copy the underlying char[] array (relying on immutability of the otherString); you have to do new String(otherString.toCharArray()) if you really want to protect against it.

  • (nodebb) in reply to D

    Don't tell him that! Imagine he hears about it and sprinkles the array duplication everywhere. That'd be a huge commit.

  • (nodebb) in reply to Naomi

    #define TRUE FALSE

  • Mr. Cranky Pants (unregistered)

    Any developer who names an argument the same as a member variable, such that [s]he needs to disambiguate by qualifying one with the 'this' pointer (i.e. this.value = value;) needs to be taken behind the server rack and strangled with a Cat 5 cable.

  • sizer99 (google)

    Real hell for programmers is a terrible GUI framework, like Win32 MFC or the early Java frameworks.

  • Free Bird (unregistered) in reply to Mr. Cranky Pants

    Why? For constructors and setters, that's the normal approach. I can understand why you wouldn't like it in other contexts, but this is so standard it should be second nature to even moderately experienced developers.

  • xtal256 (unregistered) in reply to Mr. Cranky Pants

    So instead is should be this.value = thatOtherValue?

  • VI (unregistered) in reply to P

    Oh boy, this mistake (mishandling String references and creating tons of String objects as a result) is covered even in OCJP material and exam questions. That should tell you something.

    Tells me that the API is lacking, because the "obvious thing to do (for the untrained)" is the wrong thing to do. In this case, there's no actual reason for String to have a copy-constructor.

  • Pedro (unregistered)

    string _myString; public string myString { get {return string.IsNullOrEmpty(_myString) ? null : _myString; } set { _myString = value; } }

    ... but really a simple "public string myString {get; set; }" would do ...

  • Pedro (unregistered)
    string _myString;
    public string myString {
        get {return string.IsNullOrEmpty(_myString) ? null : _myString; }
        set { _myString = value; } }
    

    ... but really a simple "public string myString {get; set; }" would do ...

    This forum needs a "preview" button. And some formatting instructions for noobs like me.

  • An Intelligent Observer (unregistered)

    .length is a method, not a property of a Java string. He should call .length() instead

  • Cid (unregistered) in reply to An Intelligent Observer

    Sorry, my fault, while retyping this on TDWTF. In the original sources, it was "length()", of course.

  • (nodebb)

    Actually, the real WTF is that the author thinks an empty string is the same as a null string and a string consisting only of white space is the same as both.

  • EwgB (unregistered) in reply to Mr. Cranky Pants

    Why don't you tone it down a notch there, buddy? That is a very strong opinion on a question not deserving of such a vitriol one way or another. It's not like it's tabs vs. spaces, is it?

    And by the way, you are wrong and I'm willing to fight you about it! ;-)

Leave a comment on “An Ugly Mutation”

Log In or post as a guest

Replying to comment #:

« Return to Article