[Moving top posted response down]
Chris Smith <
[email protected]> wrote in message
1. public static String getData(String var,float mFactor) {
if (var == null || var.trim().length() == 0) {
return null;
}
try {
Pattern pattern = PatternManager.getDollarAmountPattern();
if (pattern==null) return var;
PatternMatcher matcher = new Perl5Matcher();
if (matcher.contains(var, pattern)) {
MatchResult result = matcher.getMatch();
var = result.group(2);
if (result.group(2) == null || result.group(2).trim().length() < 1)
return var;
else {
copay=String.valueOf(Math.round(Float.parseFloat(var)*mFactor));
}
if (result.group(1) != null && result.group(1).trim().length() > 0) {
return result.group(1) + var;
}
else if (result.group(3) != null && result.group(3).trim().length() >
0) {
return result.group(2) + result.group(3);
}
else {
return "$" + result.group(2);
}}}
catch (NumberFormatException ex) {}
return copay;
}
2. result.group(1/2/3) is a method used to extract the part of the
input pattern. It is a standard method of jakarta ORO (pattern
matching)..
3.My question was which is a better coding style if I have to call a
method thrice then should I use a temporary variable as is done in
your solution?
Please do not top-post. It is difficult to follow the thread.
The code you have posted this time adds something to the previous
version, but isn't still clear enough. The variable "var" is now known
to be local, but you've introduced a new one called "copay" which isn't
declared locally. So we have to assume that it is an instance variable -
Is this correct ?
There is only one suggestion I can provide - though that has more to do
with improving the readability of the code rather than optimization
(Though I suspect it might improve performance as well) :
When you use the same complex expression a multiple number of times in
any section of code, it is worthwhile to move it into a separate method.
In your code, the expression "someString != null &&
someString.trim().length() > 0" (and the negation of the same
expression) occurs in 4 places, so it is well worth it to move it into a
method.
Since you're using jakarta libraries anyway, you'll find that the same
function is available as "isNonBlank()" and the negated function as
"isBlank()" in the org.jakarta.commons.lang.StringUtils class. Also that
library implements it slightly more efficiently (checks for strings that
are all whitespace without producing an intermediate String object using
trim()).
And the answer to your point no. (3) : In my opinion it is always better
to use a temporary variable than calling a function more than once on
the fly. Introducing a variable only adds an additional reference slot
in your stack frame - whereas calling the method multiple number of
times will most probably create additional Objects as well - more work
for the garbage collector, as well as use up extra execution time (which
may matter if the method in question is complex).
Of course the above will apply only for method calls that will yield the
same result when called multiple number of times. Not for methods such
as iterator.next()
BK
BK