avoiding arbitrary local variable names

R

Roedy Green

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();
 
L

Larry A Barowski

Roedy Green said:
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.
 
M

Mike Schilling

Roedy Green said:
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();


You could write this more simply as

final String freshWeb2 =
PLAINHREF + "\"http://" .... + "</a>";

The Java compiler will generate the StringBuilder calls for you.
 
M

Mark Rafn

Roedy Green said:
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();

final StringBuilder sb3 = new StringBuilder( 150 );
sb3.append(
"<span class=\"unobtrusive\">or possibly from your
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.
 
O

Owen Jacobson

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();

JSPs.

Cheers,
-o
 
G

Graham

Roedy said:
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();

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
 
E

Eric Sosman

Roedy said:
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();

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();
 
R

Roedy Green

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.
 
R

Roedy Green

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.
 
M

Mike Schilling

Roedy Green said:
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.

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 ? : ,

Or with private methods.
but
the code becomes unreadable pretty quickly.

Less so.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top