Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

Could this code snippet

StringBuilder builder = new StringBuilder();
builder.Append("Have a ");
builder.Append("nice day!");

be better written like this?

  StringBuilder builder = new StringBuilder();
  builder.Append("Have a ")
         .Append("nice day!");

In C# and Java, what would a better way to write it be?

share|improve this question

closed as not constructive by Jon, T.J. Crowder, thumbmunkeys, Rawling, Rune FS Feb 25 '13 at 9:15

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. If this question can be reworded to fit the rules in the help center, please edit the question.

    
Internally all boils down to same thing in memory. But redability wise, i would go for 1st approach. –  zenwalker Feb 25 '13 at 9:13
    
There is no "better". The APIs are just somewhat different; the second option would be good if you can provide it consistently across all of StringBuilder's API (otherwise it just seems arbitrary if only some methods allow chaining). –  Jon Feb 25 '13 at 9:14
    
I usually prefer a style which my IDE supports to auto styling. So 1st is better. –  deepmax Feb 25 '13 at 9:14
3  
Coding style questions are inherently subjective, I'm afraid. Subjective questions are not what SO is for, see the FAQ for details. –  T.J. Crowder Feb 25 '13 at 9:14
    
I like the first approach, it's easier to debug IMO. –  zmbq Feb 25 '13 at 9:14

1 Answer 1

up vote 0 down vote accepted

The pattern in the 2nd example is a "fluent API" / "fluent interface"; frankly neither is strictly better - but if the second exists it can be slightly inefficient not to use it. In .NET IL terms, the first syntax involves an extra ldloc and pop per iteration, which the second avoids - but that is rarely hugely significant. Either will work fine.

Personally, I would optimize for readability and convenience unless you know it is in a performance-critical spot.

Indeed, you could just use:

string s = "Have a " + "nice day!";

which the compiler (in C#, at least - I don't know about java) will compute at compile-time (since they are both constants) and compile down to a single ldstr (which is automatically interned, too).

share|improve this answer
    
Using + instead of StringBuilder's append() method creates a new String object on the heap (at least for variable Strings), which we want to avoid if using StringBuilder. –  alan.sambol Feb 25 '13 at 9:46
    
@alan.sambol that depends on usage; for example, in C#, the statement: string s = name + ": " + count + " - " + comment; is actually a single call to string.Concat, so only results in 1 additional string instance - the one we want. You can't do any better than that. If we were concatenating in a loop, then yes: StringBuilder would be ideal. The question does not show that, though. –  Marc Gravell Feb 25 '13 at 9:53
    
That code snippet will work in Java. –  Jared Nielsen May 3 '13 at 19:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.