• 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
    Comment held for moderation.
  • 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)
    Comment held for moderation.
  • adam smith (unregistered)
    Comment held for moderation.
  • An Intelligent Observer (unregistered)
    Comment held for moderation.

Leave a comment on “An Ugly Mutation”

Log In or post as a guest

Replying to comment #:

« Return to Article