• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Using Formatter + StringBuilder together

 
Ranch Foreman
Posts: 77
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Can someone please show me how to use StringBuilder with String.Format features, so I can use the format specifiers? Is this a thing? Or should I just use normal concatenation?

Thanks T.
 
Ranch Hand
Posts: 106
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Antonio Moretti wrote:Hi

Can someone please show me how to use StringBuilder with String.Format features, so I can use the format specifiers? Is this a thing? Or should I just use normal concatenation?

Thanks T.



There is a bit of confusion here. StringBuilder and the String.format serve entirely different purposes. If you want format specifiers, ignore StringBuilder.

https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/lang/String.html#format(java.lang.String,java.lang.Object...)

You can use the output of String.format as an input to StringBuilder.

For what its worth, I prefer to see a nicely designed format specifier template than a hodge-podge of randomly concatenated + strings. I feel String.format is the way to go and I always use it even when +ing strings might work.
Also its even worse if you hide the +Strings chain behind a nasty long unreadable chain of StringBuilder.appends().
 
Saloon Keeper
Posts: 13366
295
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not uncommon to use a StringBuilder to build a multi-line string, where you might sometimes want to format one or more of the individual lines. I see no problem doing something like this:
 
Saloon Keeper
Posts: 24497
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:I see no problem doing something like this:



I do:
As I've said more than once, naked dependent clauses are trouble waiting to happen. And in the case of two builder.append() calls immediately adjacent to one another, people can get all sorts of false impressions.
 
Marshal
Posts: 74341
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I am used to seeing multiple StringBuilder#append() calls. Maybe you would like to explain the problem a bit more, Tim.

Note for those unfamiliar with StringBuilder: the append() method is overloaded with over ten versions.
 
Tim Holloway
Saloon Keeper
Posts: 24497
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Well, I am used to seeing multiple StringBuilder#append() calls. Maybe you would like to explain the problem a bit more, Tim.

Note for those unfamiliar with StringBuilder: the append() method is overloaded with over ten versions.



Yeah, I don't really see the String.format() method adding much value over a set of chained appends myself. But that's an even better illustration of my complaint:

Now visualize some gormless twit "correcting" this code to change the first "append(NEWLINE);" to "append(NEWLINE).". And, of course delete the next "builder." because it didn't need to be there.

I've done worse things to my own code, I'm sorry to say.
 
Saloon Keeper
Posts: 8704
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I'm preferring the format() version.
 
Saloon Keeper
Posts: 1606
51
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:

Yeah, I don't really see the String.format() method adding much value over a set of chained appends myself. But that's an even better illustration of my complaint:

Now visualize some gormless twit "correcting" this code to change the first "append(NEWLINE);" to "append(NEWLINE).". And, of course delete the next "builder." because it didn't need to be there.

I've done worse things to my own code, I'm sorry to say.



I am relatively neutral on naked for clauses, and I know this is not the point of this part of the thread, but I have worked with people who would get mad or fail code on a code review because they are sticklers for { }, so I will ask:

Would we have the same gormless twit correction liability if we had written?


I'm asking for a friend.  
 
Stephan van Hulst
Saloon Keeper
Posts: 13366
295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:I have worked with people who would get mad or fail code on a code review because they are sticklers for { }


People who get mad at this at a code review should be mad at themselves for not installing a style checker in the continuous integration process.
 
Stephan van Hulst
Saloon Keeper
Posts: 13366
295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Yeah, I don't really see the String.format() method adding much value over a set of chained appends myself.


Please take a look at Carey's version. It more accurately represents my format string.
 
Tim Holloway
Saloon Keeper
Posts: 24497
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:
I am relatively neutral on naked for clauses, and I know this is not the point of this part of the thread, but I have worked with people who would get mad or fail code on a code review because they are sticklers for { }, so I will ask:

Would we have the same gormless twit correction liability if we had written?


I'm asking for a friend.  



Very unlikely. The braces are "speed bumps" and make it more obvious that we are shifting contexts. Your preferred placement of braces, incidentally, is secondary. The bracketing is more important than where you place it - unless your indentation scheme is so annoying that people can't see the code because they're distracted by the code formatting!

The reason I get so adamant about explicit enclosure of dependent clauses is when I have to add debugging code and end up doing things like this:

You'd say only an idiot would do this, but...

More commonly, though, I've introduced whole new sets of bugs by doing less obvious things of the same vein. Especially on "if" statments and very especially on "else" clauses. Call it anti-bugging. Call it cheap insurance. Just don't call me if you didn't do it and weird things start happening.
 
Jesse Silverman
Saloon Keeper
Posts: 1606
51
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found it interesting that Perl 5 forbade:

if (condition) doSomething();

but then before you get off of the beginner's book, you see you can say:

doSomething() if (condition)

I haven't written Perl for a year, forgive any spelling or grammar errors.

For a long time I felt that was just silly, but perhaps the latter is far, far less likely to be misread as including succeeding statements when it does not?

Regardless, I've worked closely enough with individuals adamant enough about no if/for/while/whatever without braces that I use them automatically unless something fits on one line and ends with a ;
Even there I normally only do it in private.

Boy, have I worked with people who felt strongly about braces when it came to code safety!  Perhaps they were of the "Belts and Braces" school...the relevant point here was that I have internalized it, as I would rather get yelled at for some new and interesting thing I did wrongly than something I can just remember a rule to prevent.  It is so rare for me to omit them it is difficult to speculate how often doing so would cause me to shoot myself somewhere south of the border.

 
Marshal
Posts: 3707
523
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:unless something fits on one line and ends with a ;


If the code ever gets touched by a formatter, there is a good chance that the one line will become two lines.
 
Tim Holloway
Saloon Keeper
Posts: 24497
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perl has 2 constructs: "do-something IF" and "do-something UNLESS". I wouldn't bracket on these since I consider them as all part of the same statement, as opposed to if/then.

And in fact, in Perl if I can't make the "do-something" a simple statement, I use a standard if/then/else construct.
 
Campbell Ritchie
Marshal
Posts: 74341
334
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The great advantage of using format() is that it makes it much easier to spread text out with whitespace to align in columns, and (for the same reason) to specify number of digits shown for numbers.
 
Tim Holloway
Saloon Keeper
Posts: 24497
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:The great advantage of using format() is that it makes it much easier to spread text out with whitespace to align in columns, and (for the same reason) to specify number of digits shown for numbers.


Yeah, I really shouldn't have said that since most of the time that's my preferred approach as well. Was just trying to get a better frame for what I actually wanted to illustrate. Also, I think that the escaped quotes were distracting me - you may notice they didn't register properly, since I didn't even include them in my "append" version.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic