HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Simplifying ToString() implementation

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
implementationtostringsimplifying

Problem

Any ideas?

public override string ToString() {
    string returnValue;
    if (!string.IsNullOrWhiteSpace(Country))
        returnValue = string.Format("{0}", Country);
    else
        return returnValue;

    if (!string.IsNullOrWhiteSpace(City))
        returnValue += string.Format(", city {0}", City);
    else
        return returnValue;

    if (!string.IsNullOrWhiteSpace(Street))
        returnValue += string.Format(", street {0}", Street);
    else
        return returnValue;

    if (!string.IsNullOrWhiteSpace(Block))
        returnValue += string.Format(", block {0}", Block);
    else
        return returnValue;

    if (!string.IsNullOrWhiteSpace(Building))
        returnValue += string.Format(", building {0}", Building);

    if (!string.IsNullOrWhiteSpace(Latitude) 
             && !string.IsNullOrWhiteSpace(Longitude))
    returnValue += string.Format(", coordinates {0}:{1}", Latitude, Longitude);

    return returnValue;
}

Solution

I find that writing it this way makes it more maintainable and relatively efficient.

public class Information
{
    public string Country { get; set; }
    public string City { get; set; }
    public string Street { get; set; }
    public string Block { get; set; }
    public string Building { get; set; }
    public string Latitude { get; set; }
    public string Longitude { get; set; }

    private IEnumerable> DataFormatPairs()
    {
        yield return Tuple.Create(new[] { Country },             "{0}");
        yield return Tuple.Create(new[] { City },                "city {0}");
        yield return Tuple.Create(new[] { Street },              "street {0}");
        yield return Tuple.Create(new[] { Block },               "block {0}");
        yield return Tuple.Create(new[] { Building },            "building {0}");
        yield return Tuple.Create(new[] { Latitude, Longitude }, "coordinates {0}:{1}");
    }
    public override string ToString()
    {
        var data = DataFormatPairs()
            .TakeWhile(p => p.Item1.All(s => !String.IsNullOrWhiteSpace(s)))
            .Select(p => String.Format(p.Item2, p.Item1));
        return String.Join(", ", data);
    }
}

Code Snippets

public class Information
{
    public string Country { get; set; }
    public string City { get; set; }
    public string Street { get; set; }
    public string Block { get; set; }
    public string Building { get; set; }
    public string Latitude { get; set; }
    public string Longitude { get; set; }

    private IEnumerable<Tuple<string[], string>> DataFormatPairs()
    {
        yield return Tuple.Create(new[] { Country },             "{0}");
        yield return Tuple.Create(new[] { City },                "city {0}");
        yield return Tuple.Create(new[] { Street },              "street {0}");
        yield return Tuple.Create(new[] { Block },               "block {0}");
        yield return Tuple.Create(new[] { Building },            "building {0}");
        yield return Tuple.Create(new[] { Latitude, Longitude }, "coordinates {0}:{1}");
    }
    public override string ToString()
    {
        var data = DataFormatPairs()
            .TakeWhile(p => p.Item1.All(s => !String.IsNullOrWhiteSpace(s)))
            .Select(p => String.Format(p.Item2, p.Item1));
        return String.Join(", ", data);
    }
}

Context

StackExchange Code Review Q#4669, answer score: 2

Revisions (0)

No revisions yet.