E
< quote user="PeaceLove&WTF" >
I have a quick question about your code example though. Assuming the
String "a" is read later on in the code, would Sun's optimizer simply
just omit the first two lines of concatenation? They have no effect on
the program, and it seems like this is something a reasonbly well
written optimizer should be able to determine (I understand what you're
trying to demonstrate, just asking 'cos you seem to know a bit about
optimizers).
< /quote >
I'm not quite sure if I understand your question. Do you mean this:
a = expr1;
a = expr2;
a = expr3; // now assignments from expr1 and expr2 are just superfluous!
??
If so, then yes, if expr1 and expr2 do not have side-effects, they
could be optimized away, yet I do not know if any JIT-Compiler does
this.
< quote user="PeaceLove&WTF" >
Also, in your third concatenation example, may I suggest:
a = b + (what ? c : "") + d;
This is, IMO, the most readable way to accomplish what we're trying to
accomplish (it clearly shows that regardless of what "what" is, b and d
are going to be the front and back of the string, and c may be added,
depending on the value of "what"). Also, it's only marginally less
optimal than your example (i.e., when "what" is false, we're still
stuck with two appends, rather than just one).
< /quote >
First of all it should be
a = b + (what ? "" : c) + d;
Well, sure this works for this simplified example, but the point I
wanted to illustrate, are the two levels of StringBuilders introduced
through inline conditionals with subexpressions. Perhaps I should have
used this nonreducable example:
a = (what ? "Looks like " + b + " is element of " : "Looks like "+b + " and "+ c +" are elements of ") + d +".";
But wouldn't you agree, it would have distracted from the point to be illustrated?
cu
(And, yes I know, this last example is bad code regarding internationalization, but it's an example, for god's sake!)
(And yes, I know how to quote, but this f**king forum SW always rejected my post: <font color="Red">Non matching quote blocks in post.</font>Where?)