6
\$\begingroup\$

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; } 
\$\endgroup\$
4
  • 2
    \$\begingroup\$ You did notice that in all but the first case you are returning strings. But the first case return Country you are always returning a NULL or an empty string. In which case you may want to make that explicit return string.empty just to show that it is nothing that is being returned. \$\endgroup\$ Commented Sep 8, 2011 at 15:28
  • \$\begingroup\$ I'm sorry for the typo. It must be "return returnValue;" \$\endgroup\$ Commented Sep 9, 2011 at 6:53
  • \$\begingroup\$ @Alex About your Update: There are ways to simplify it, just depends on what you put a higher priority, readability/maintainability or performance etc etc. Your code looks easy to maintain but it could definitely be shorter Or run quicker ... did that make sense? So what exactly did you want simpler? \$\endgroup\$ Commented Sep 13, 2011 at 0:02
  • \$\begingroup\$ I do not know what is a higher priority. It's and a readbility and a perfomance. \$\endgroup\$ Commented Sep 13, 2011 at 7:06

4 Answers 4

2
\$\begingroup\$

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<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); } } 
\$\endgroup\$
1
\$\begingroup\$

Using "&&" it will stop the evaluation at the first false, so something like this should work for you:

public class MyClass { public string Item1 { get; set; } public string Item2 { get; set; } public string Item3 { get; set; } public string Long { get; set; } public string Lat { get; set; } public override string ToString() { var sb = new StringBuilder(); var tempVar = (AddPropertyData(sb, "{0}", Item1) && AddPropertyData(sb, ", item2 {0}", Item2) && AddPropertyData(sb, ", item3 {0}", Item3)) && AddPropertyData(sb, ", long: {0}, lat: {1}", Long, Lat); return sb.Length > 0 ? sb.ToString() : Item1; } private static bool AddPropertyData(StringBuilder sb, string format, params string[] data) { if (data.All(x => !String.IsNullOrWhiteSpace(x))) { sb.AppendFormat(format, data); return true; } return false; } } 
\$\endgroup\$
1
  • \$\begingroup\$ First question is, why isn't your AddPropertyData static? Then, what do you do with "coordinates" where the single value consists of 2 members (latitude, longitude)? \$\endgroup\$ Commented Sep 8, 2011 at 13:34
1
\$\begingroup\$

Here's another variation:

public override string ToString() { var list = new List<string>(); if (!string.IsNullOrWhiteSpace(Country)) list.Add(string.Format("{0}", Country)); if (!string.IsNullOrWhiteSpace(City)) list.Add(string.Format("city {0}", City)); if (!string.IsNullOrWhiteSpace(Street)) list.Add(string.Format("street {0}", Street)); if (!string.IsNullOrWhiteSpace(Block)) list.Add(string.Format("block {0}", Block)); if (!string.IsNullOrWhiteSpace(Building)) list.Add(string.Format("building {0}", Building)); if (!string.IsNullOrWhiteSpace(Latitude) && !string.IsNullOrWhiteSpace(Longitude)) list.Add(string.Format("coordinates {0}:{1}", Latitude, Longitude)); return list.Count > 0 ? String.Join(", ", list.ToArray()) : string.Empty; } 
\$\endgroup\$
1
  • 1
    \$\begingroup\$ What is the difference between your code and my code? \$\endgroup\$ Commented Sep 9, 2011 at 6:51
0
\$\begingroup\$

I would do something like this:

 public override string ToString() { string returnValue = string.empty; if (string.IsNullOrWhiteSpace(Country)) return returnValue; // Note: this is a change from the original code // That could have returned NULL. But I think the // intent of the function is to always return some // form of string. returnValue = string.Format("{0}", Country); if (string.IsNullOrWhiteSpace(City)) return returnValue; returnValue += string.Format(", city {0}", City); if (string.IsNullOrWhiteSpace(Street)) return returnValue; returnValue += string.Format(", street {0}", Street); if (string.IsNullOrWhiteSpace(Block)) return returnValue; returnValue += string.Format(", block {0}", Block); 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; } 

If you want to compress this slightly. Though I am in two minds weather this is worth it or not:

public class Pair { public Pair(string v, string f) { this.Value =v; this.Format = f;} public string Value { get; private set; } public string Format { get; private set; } }; public override string ToString() { string returnValue = string.empty; string[] address = {new Pair(Country, "{0}"), new Pair(City, ", city {0}"), new Pair(Street, ", street {0}"), new Pair(Block, ", block {0}") }; foreach(Pair loop in address) { if (string.IsNullOrWhiteSpace(loop.Value)) return returnValue; returnValue += string.Format(loop.Format, loop.Value); } 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; } 
\$\endgroup\$
8
  • \$\begingroup\$ -1: I posted 2 answers in this topic and your answer is exact duplicate of what I recommend. \$\endgroup\$ Commented Sep 8, 2011 at 16:40
  • 1
    \$\begingroup\$ Moreover, using string for appending is a bad practice - it's way slower than StringBuilder. \$\endgroup\$ Commented Sep 8, 2011 at 16:46
  • \$\begingroup\$ @loki2302: I'll take the -1 for not using StringBuilder. But don't vote people down because they have the same idea as you. Anyway this code is superior to yours in the maintainability field. Yours fails on the front that you are trying to hard to be clever and it makes the code less readable. \$\endgroup\$ Commented Sep 8, 2011 at 20:06
  • 2
    \$\begingroup\$ The idea of this site is to have questions and answers. I see no reason in having the same answer for the same question twice. Read before you post - it's about 3 hours between mine and yours: you had a chance to see that everything you wanted to say has already been said. Not also sure why you think your code is more maintainable: redundant class, redundant logic. My approach is short, stupid and it works. It's really easy to understand and modify. I see no reason why someone might want to have extra ifs when the problem is easily solvable with less code and less logic. \$\endgroup\$ Commented Sep 8, 2011 at 20:33
  • 2
    \$\begingroup\$ I'm not trying to say mine is better, I just see complexity and redundancies in yours. Re-read your code. \$\endgroup\$ Commented Sep 8, 2011 at 20:58

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.