Jon said:
Ok.
It is better because:
1) It is clearer as to the intent. Most experts say to write for clarity
first.
I'd say that writing "s" + (a==0 ? "foo" : a) is clearer to the intent.
2) It is an 'optimization' that usually doesn't make any noticeable
difference, and is very commonly premature.
So use a tool to optimize: a lot of Java would greatly benefit from a
preprocessor/macro expander. If you put the optimization into a tool,
that expands a generic form, then you save time optimizing by hand.
3) It helps reduce bugs.
4) It makes the code hold up even if the type of the variable changes.
5) It allows the code to be copy-n-pasted and reused without having to
be changed. Reduces rogue tile occurances.
They are identical in this respect.
6) On the fastest VM's, doing it that way can be faster. (although you
should note point 2)
Is the IBM VM faster when running on OS X? I couldn't find any benchmarks
...
There, is that better?
I think you missed the point, and I maybe would have been better to post
without the aside on the extra method indirections; the main gain I was
driving at was to allow a tool to expand the generic form where deciding
the type can be left until the expression is used (given that changing
the language seems a bit optimistic). OK, so if you're targeting one
particular vendor's JVM, then a small gain can be made by customizing
the expansion in a different way than what is better for the standard one.
You seem to misunderstand. It's all in Java-land, not native land. Also
it does nothing to break use, and does not introduce any other problems
to do so.
Yes, not having had any reason to inspect the 1.1 implementation of the
IBM libraries, I misunderstood your use of internal, and assumed it was
internal to the JVM implementation not it's libraries.
The reference implementation is just to be consulted on points on which
the language spec does not cover. Since it does in fact cover this
point, the reference implementation does not trump it.
Neither the language spec nor the API spec cover whether it's any
'better'. The reference implementation does indicate that it's
(marginally) better to expand one way rather than the other in terms of
calls, so I chose to do it that way, which AFAI can tell is better on
the JVMs I get to use.
I disagree. If you need to change a variable declaration, it should not
require a tool to assist you to do so. If that is the case, it sounds
like your code is probably too fragile and unmaintainable.
In the absence of a customisable compiler, writing a tool that expands
<String> + (<bool> ? <type A> : <type B>)
into the StringBuffer calls I suggested would make it less fragile, not
more, for reasons you've already pointed to.
Such a tool should also be able to pick up on forms such as
String s = "foo";
if (a != 3) {
s += a;
}
s += "bar";
Which could use only the one StringBuffer instead of the two that javac
does (the one in the OS X JDK, maybe IBM's compiler already has this
covered, I don't know, they don't do OS X developer kits).
Pete