What are your options for code like this so you don't have to make up
arbitrary local variables names, sb2/sb3?
final StringBuilder sb2 = new StringBuilder( 150 );
sb2.append( PLAINHREF );
sb2.append( "\"http://" );
sb2.append( com.mindprod.htmlmacros.Configure.WEBSITE_DOMAIN
);
sb2.append( "/" );
sb2.append( name );
sb2.append( "\">http://" );
sb2.append( com.mindprod.htmlmacros.Configure.WEBSITE_DOMAIN
);
sb2.append( "/" );
sb2.append( name );
sb2.append( "</a>" );
final String freshWeb2 = sb2.toString();
final StringBuilder sb3 = new StringBuilder( 150 );
sb3.append(
"<span class=\"unobtrusive\">or possibly from your
local " );
sb3.append( PLAINHREF );
sb3.append( "\"" );
sb3.append( jDriveLink );
sb3.append(
"\">J: drive</a> (Java virtual drive/Mindprod website
mirror)</span>" );
final String freshLocal1 = sb3.toString();

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Larry A Barowski - 18 Dec 2007 23:33 GMT
> What are your options for code like this so you don't have to make up
> arbitrary local variables names, sb2/sb3?
> ...
I like to tack the purpose on to the end: freshWeb2Scratch,
freshLocal1Scratch. Similarly, for array indices, fileIndex,
recordIndex, etc. rather than i, j, k. The goal is to make the
purpose of each variable fairly evident, even if the entire
context is not visible.
Mike Schilling - 19 Dec 2007 00:34 GMT
> What are your options for code like this so you don't have to make up
> arbitrary local variables names, sb2/sb3?
[quoted text clipped - 13 lines]
> sb2.append( "</a>" );
> final String freshWeb2 = sb2.toString();
You could write this more simply as
final String freshWeb2 =
PLAINHREF + "\"http://" .... + "</a>";
The Java compiler will generate the StringBuilder calls for you.
Roedy Green - 19 Dec 2007 21:13 GMT
On Tue, 18 Dec 2007 16:34:40 -0800, "Mike Schilling"
<mscottschilling@hotmail.com> wrote, quoted or indirectly quoted
someone who said :
>The Java compiler will generate the StringBuilder calls for you.
If you let the compiler generate your StringBuilders, you won't get
the original size estimate correct. If you have embedded IF s in the
construction of the string. each piece gets it own StringBuilder
rather than tacking onto the original.
e.g.
if ( special )
{
sb.append( "something ");
sb.append( value );
}
else
{
String usual = getUsual();
if (usual != null )
{
sb.append( usual );
}
}
To a certain extend you can dodge the problem with nested ? : , but
the code becomes unreadable pretty quickly.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Mike Schilling - 20 Dec 2007 01:35 GMT
> On Tue, 18 Dec 2007 16:34:40 -0800, "Mike Schilling"
> <mscottschilling@hotmail.com> wrote, quoted or indirectly quoted
[quoted text clipped - 4 lines]
> If you let the compiler generate your StringBuilders, you won't get
> the original size estimate correct.
Unless that's a bottleneck, I can live with it.
If you have embedded IF s in the
> construction of the string. each piece gets it own StringBuilder
> rather than tacking onto the original.
[quoted text clipped - 16 lines]
>
> To a certain extend you can dodge the problem with nested ? : ,
Or with private methods.
> but
> the code becomes unreadable pretty quickly.
Less so.
Mark Rafn - 19 Dec 2007 00:35 GMT
>What are your options for code like this so you don't have to make up
>arbitrary local variables names, sb2/sb3?
For a small scope, I have no objection to short names. If it's more than one
screenful, it's time to refactor or rename!
> final StringBuilder sb2 = new StringBuilder( 150 );
> sb2.append( PLAINHREF );
> sb2.append( "\"http://" );
... [ more appends ]
> final String freshWeb2 = sb2.toString();
>
[quoted text clipped - 3 lines]
>local " );
> sb3.append( jDriveLink );
... [ more appends ]
> final String freshLocal1 = sb3.toString();
For temporary local variables that don't leak into other functions or inner
classes, I usually don't make them final, and reuse them as needed.
StringBuilder sb = new StringBuilder(150);
... [ append stuff ]
String freshWeb2 = sb.toString();
sb = new StringBuilder(150);
... [ append stuff ]
String freshLocal1 = sb.toString();
... etc.
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>
Owen Jacobson - 19 Dec 2007 01:02 GMT
On Dec 18, 3:14 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> What are your options for code like this so you don't have to make up
> arbitrary local variables names, sb2/sb3?
[quoted text clipped - 25 lines]
> mirror)</span>" );
> final String freshLocal1 = sb3.toString();
JSPs.
Cheers,
-o
Graham - 19 Dec 2007 09:33 GMT
> What are your options for code like this so you don't have to make up
> arbitrary local variables names, sb2/sb3?
[quoted text clipped - 13 lines]
> sb2.append( "</a>" );
> final String freshWeb2 = sb2.toString();
The following code snip gives an alternative example structure which you
might consider:
final String freshWeb2 = new StringBuilder( 150 )
.append( PLAINHREF )
.append( "\"http://" )
.append( com.mindprod.htmlmacros.Configure.WEBSITE_DOMAIN )
.append( "/" )
.append( name )
.append( "\">http://" )
.append( com.mindprod.htmlmacros.Configure.WEBSITE_DOMAIN )
.append( "/" )
.append( name )
.append( "</a>" )
.toString();
- Graham
Eric Sosman - 19 Dec 2007 13:51 GMT
> What are your options for code like this so you don't have to make up
> arbitrary local variables names, sb2/sb3?
[quoted text clipped - 25 lines]
> mirror)</span>" );
> final String freshLocal1 = sb3.toString();
One thought is to jettison some of those `final's and
re-use the same identifier for as many StringBuilders as
you like. (A StringBuilder, after all, is usually a "helper"
object rather than a "goal" object.)
StringBuilder sb = new StringBuilder(150);
sb.append(...);
...
final String freshWeb2 = sb.toString();
sb /* same ident */ = new StringBuilder(150);
sb.append(...);
...
final String freshLocal1 = sb.toString();

Signature
Eric Sosman
esosman@ieee-dot-org.invalid
Roedy Green - 19 Dec 2007 21:16 GMT
On Tue, 18 Dec 2007 23:14:30 GMT, Roedy Green
<see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted
someone who said :
>What are your options for code like this so you don't have to make up
>arbitrary local variables names, sb2/sb3?
I'm impressed with all the original thinking to this question. You
cleverly did not just answer the answer literally, but saw the
underlying problem.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com