Re: Please tighten up these 10 lines of Java code
On Aug 13, 8:06 pm, "metaperl....@gmail.com" <metaperl....@gmail.com>
wrote:
Ok, I just want to know if you have any suggestions for improving this
code. I dont like typing System.out before all my I/O calls but I
cant extend System.out because I am extending DepthFirstAdapter.
Actually, You can't and shouldn't extend System.out.
You can't extend it because its not a class, but a reference to a
static object.
You shouldn't extend it, because extending something just to type
less defeats the purpose of inheritance and polymorphism.
I dont like calling print() one line and then println() on the next.
Something like printf would be nice.
You can build the string before hand, or as people have suggested, you
can use string concatenation.
Any other suggestions welcome.
[snip]
Suggestions: Make ASexpExpr have an array (or collection) of Args,
rather than arg1 and arg2. If you find yourself adding a number to
the end of a variable, then it is most likely better to have it as an
array or collection.
After that, you can have getArg(int whichArg) and others:
public double getDoubleArg(int whichArg) {
return Double.parseDouble(getStringArg(whichArg));
}
public String getStringArg(int whichArg) {
return String.valueOf(getArg(whichArg));
}
So your sum becomes:
System.out.println("Their sum: " + (node.getDoubleArg(0) +
node.getDoubleArg(1)));