avoiding arbitrary local variable names

Discussion in 'Java' started by Roedy Green, Dec 18, 2007.

  1. Roedy Green

    Roedy Green Guest

    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
    Roedy Green, Dec 18, 2007
    #1
    1. Advertising

  2. "Roedy Green" <> wrote in message
    news:...
    > 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.
    Larry A Barowski, Dec 18, 2007
    #2
    1. Advertising

  3. "Roedy Green" <> wrote in message
    news:...
    > 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.
    Mike Schilling, Dec 19, 2007
    #3
  4. Roedy Green

    Mark Rafn Guest

    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?


    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.dagon.net/>
    Mark Rafn, Dec 19, 2007
    #4
  5. On Dec 18, 3:14 pm, 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();


    JSPs.

    Cheers,
    -o
    Owen Jacobson, Dec 19, 2007
    #5
  6. Roedy Green

    Graham Guest

    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
    Graham, Dec 19, 2007
    #6
  7. Roedy Green

    Eric Sosman Guest

    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
    lid
    Eric Sosman, Dec 19, 2007
    #7
  8. Roedy Green

    Roedy Green Guest

    On Tue, 18 Dec 2007 16:34:40 -0800, "Mike Schilling"
    <> 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
    Roedy Green, Dec 19, 2007
    #8
  9. Roedy Green

    Roedy Green Guest

    On Tue, 18 Dec 2007 23:14:30 GMT, Roedy Green
    <> 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
    Roedy Green, Dec 19, 2007
    #9
  10. "Roedy Green" <> wrote in message
    news:...
    > On Tue, 18 Dec 2007 16:34:40 -0800, "Mike Schilling"
    > <> 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.
    Mike Schilling, Dec 20, 2007
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Honestmath
    Replies:
    5
    Views:
    549
    Honestmath
    Dec 13, 2004
  2. wanwan
    Replies:
    3
    Views:
    415
    Alex Martelli
    Oct 14, 2005
  3. dpapathanasiou

    Avoiding local variable declarations?

    dpapathanasiou, Nov 13, 2008, in forum: Python
    Replies:
    25
    Views:
    669
    Arnaud Delobelle
    Nov 19, 2008
  4. News123
    Replies:
    2
    Views:
    459
    John Machin
    Nov 26, 2008
  5. Bill  Mill
    Replies:
    13
    Views:
    170
    dhtml
    May 12, 2008
Loading...

Share This Page