Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StringBuilder, append string if conditions are met

var sb = new StringBuilder ();

if (condition1) sb.Append ("one");
if (condition2) sb.Append ("two");
if (condition3) sb.Append ("three");
if (condition4) sb.Append ("four");
if (condition5) sb.Append ("five");

return sb.ToString ();

Any idea how to improve it? How to write less code, giving same result?

like image 543
apocalypse Avatar asked Dec 19 '14 14:12

apocalypse


People also ask

Can I append string to StringBuilder?

append() method is used to append the string representation of some argument to the sequence.

Does StringBuilder pass by reference?

Because it is passed by reference. The reference to the StringBuilder is passed by value. You can add characters and they will be in the instance after the method returns. Same way you can pass a Collection and add values inside the invoke method - these will be preserved.

Can StringBuilder append int?

append(int i) method appends the string representation of the int argument to this sequence.

How does StringBuilder append work?

append(String str) method appends the specified string to this character sequence. The characters of the String argument are appended, in order, increasing the length of this sequence by the length of the argument.


2 Answers

This code is fine. Keep it like this.

  • Every attempt to use extension methods or others will only make this code less understandable and maintainable;
  • There is no repeating code;
  • As long as the conditions don't influence another, there is no way of shortening the ifs.

If you do want another option:

string s = 
   (condition1 ? "one" : null) + 
   (condition2 ? "two" : null) + 
   (condition3 ? "three" : null) + 
   (condition4 ? "four" : null) + 
   (condition5 ? "five" : null)
   ;

But let's be honest, does this make it better? No.

like image 146
Patrick Hofman Avatar answered Sep 22 '22 12:09

Patrick Hofman


I prefer approach with simple DSLs definition, when it makes code simpler or more readable. Also "pipeline-style" expressions are awesome. In your case it can be written like this:

var str =
    new StringBuilder()
        .AppendIf(condition1, "one")
        .AppendIf(condition2, "two")
        .AppendIf(condition3, "forty two")
        .ToString();

With an extension method.

public static class StringBuilderExtensions
{
    public static StringBuilder AppendIf(
        this StringBuilder @this,
        bool condition,
        string str)
    {
        if (@this == null)
        {
            throw new ArgumentNullException("this");
        }

        if (condition)
        {
            @this.Append(str);
        }

        return @this;
    }
}

Approach is suitable here, if conditions are repeated. For example arg1 != null, arg2 != null, then AppendIfNotNull can be used.

Otherwise, think twice, because it looks quite similar to initial implementation, requires additional code, can be slower because of additional null checks and method invocations, and also you should create an AppendIf overload for every Append one.

like image 22
Uładzisłaŭ Avatar answered Sep 18 '22 12:09

Uładzisłaŭ