Are 1) and 3) not precluded by the proviso "It is not acceptable to have
a special String value mean failure".
A fourth way would be to return a class/object containing both the
boolean and the String. To me, this would be the natural OO way of
returning more information than a primitive type.
And a 5th way could be to return the String and throw an Exception if it
did not work. Some purists may argue that failure to work is not
strictly an exception, but if it gets the job done...
This is the kind of thing where "purists" will get a bit bloody. Not to
teach you or any other respondent here how to suck eggs, but I'll throw
in, for general edification of a wider audience, the information that
we've got this problem both because Java has "pass object reference by
value" and Strings are immutable. Hence the OP's question.
Given that, Andreas' #2 is legit (I agree, Nigel, with your concerns
regarding #1 and #3), and so are the two suggestions you and Patricia
have put forth.
I'll accept that returning an object containing both the possible
modified String and a boolean status is OK. I don't much like it,
however, for two reasons. It's still the use of a return value for
operation status, and although that's defensible, there's nothing OO
about it - it's pure imperative programming. And were we to still pursue
that approach, unfortunately Java has no elegant means for supporting
it, unlike Haskell Maybe or Scala Option.
The failure of Java to cleanly support the previous approach pretty much
argues, IMHO, for exceptions. And this is, again IMHO, quite pure. It's
basically my business to define what I consider to be failures and
errors. Myself I see absolutely no problem in arguing that a failure to
work (parse) is a failure.
It's certainly an error in the input to
be parsed. In any case the OP would hardly be asking for an operation
success status if it wasn't possible for the method to fail.
Another argument for exceptions here is the OP's
if (!DidItWork)
// too bad
bit. I have my doubts as to whether the OP's intended error processing
consists just of a NOOP and a comment. Assuming that it doesn't, and one
actually wants to do something productive in the error-handling,
exceptions are the best way to do it.
AHS