All,
I certainly agree that the first format Neil mentions is onerous from a
readability standpoint:
> StringBuffer logBuffer = new StringBuffer("text");
> logBuffer.append(var);
> logBuffer.append("more text");
> logBuffer.append(anothervar);
> log(logBuffer.toString());
This one is somewhat better, although it tends to lead to line wraps in
unusual places or overlong lines of code:
> log(new StringBuffer(128).append(text).append(var).append("more
> text").append(anothervar).toString());
>
So the above if fine for short sets of line buffers. For longer sets
(or longer strings being concatenated), I'd suggest something like:
StringBuffer headerLineBuffer =
new StringBuffer(128)
.append("Received: from ")
.append(state.get(REMOTE_NAME))
.append(" ([")
.append(state.get(REMOTE_IP))
.append("])");
I find this vertical alignment makes the code very readable...
> However, more important than style is that although the new
> StringBuffer(128) is arbitrarily chosen for the example, it is
important
> to
> provide a reasonable initial buffer size. If you use StringBuffer(),
the
> size defaults to 16. If you use StringBuffer(String), you only get 16
> bytes
> more than the length of the initial string. You will lose the
performance
> benefits you wanted from StringBuffer, because it will have to call
> expandCapacity() multiple times.
I absolutely agree. I've taken care of it in my modifications.
--Peter
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>