Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > avoiding arbitrary local variable names

Reply
Thread Tools

avoiding arbitrary local variable names

 
 
Roedy Green
Guest
Posts: n/a
 
      12-18-2007
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();

--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
 
Larry A Barowski
Guest
Posts: n/a
 
      12-18-2007

"Roedy Green" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.


 
Reply With Quote
 
 
 
 
Mike Schilling
Guest
Posts: n/a
 
      12-19-2007

"Roedy Green" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.


 
Reply With Quote
 
Mark Rafn
Guest
Posts: n/a
 
      12-19-2007
Roedy Green <(E-Mail Removed)> wrote:
>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.
--
Mark Rafn http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.dagon.net/>
 
Reply With Quote
 
Owen Jacobson
Guest
Posts: n/a
 
      12-19-2007
On Dec 18, 3:14 pm, Roedy Green <(E-Mail Removed)>
wrote:
> 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
 
Reply With Quote
 
Graham
Guest
Posts: n/a
 
      12-19-2007
Roedy Green wrote:
> 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
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      12-19-2007
Roedy Green wrote:
> 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();

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      12-19-2007
On Tue, 18 Dec 2007 16:34:40 -0800, "Mike Schilling"
<(E-Mail Removed)> 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.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      12-19-2007
On Tue, 18 Dec 2007 23:14:30 GMT, Roedy Green
<(E-Mail Removed)> 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.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      12-20-2007

"Roedy Green" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Tue, 18 Dec 2007 16:34:40 -0800, "Mike Schilling"
> <(E-Mail Removed)> 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.


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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
checking for mis-spelled variable names / function names News123 Python 2 11-26-2008 12:37 AM
Avoiding local variable declarations? dpapathanasiou Python 25 11-19-2008 07:00 PM
Avoiding an Infinite Loop in Arbitrary eval(user_code) Bill Mill Javascript 13 05-12-2008 04:24 AM
confusion between global names and instantiated object variable names wanwan Python 3 10-14-2005 09:46 PM
calling an arbitrary function w/ arbitrary arguments Honestmath C++ 5 12-13-2004 06:18 AM



Advertisments