4
\$\begingroup\$

I have to compose an email where I have to list a list of difference between old and new value of a list of addresses. Only a subset of properties have to be listed, so the first thing that pops into my mind is doing something like this:

private static List<Difference> RetrieveDifferences(IEnumerable<AddressPoco> newValues, AddressPoco item){
  var result = new List<Difference>();
  var c = newValues.SingleOrDefault(x => x.xedsAddressID == item.xedsAddressID);
  if (c != null)
  {
    if (item.xgeoCountryID != c.xgeoCountryID){
       result.Add(new Difference("Country", item.xgeoCountryID.ToString(), c.xgeoCountryID.ToString()));
    }
    if (item.AddressTypeName != c.AddressTypeName){
       result.Add(new Difference("Address Type", item.AddressTypeName, c.AddressTypeName));
    }
    if (item.City != c.City){
       result.Add(new Difference("City", item.City, c.City));
    }
    if (item.POBox != c.POBox){
       result.Add(new Difference("POBox", item.POBox, c.POBox));
    }
    if (item.MainContact != c.MainContact){
       result.Add(new Difference("Main Contact", item.MainContact, c.MainContact));
    }
    if (item.PostCode != c.PostCode){
       result.Add(new Difference("PostCode", item.PostCode, c.PostCode));
    }
    if (item.Street != c.Street){
       result.Add(new Difference("Street", item.Street, c.Street));
    }
 }
 return result;
}

public class Difference {
  public string Key { get; set; }
  public string OldValue { get; set; }
  public string NewValue { get; set; }
  public Difference(string key, string oldValue, string newValue)
  {
    Key = key; OldValue = oldValue; NewValue = newValue;
  }
}

However, Visual Studio shows me a low maintainability index of 50 and high cyclomatic complexity in 18 rows. I can understand why. Any suggestion for how to improve it?

\$\endgroup\$
3
  • \$\begingroup\$ I would do it with reflection rather then hardcoding each property of this particular type. \$\endgroup\$
    – t3chb0t
    Commented Jul 7, 2016 at 15:14
  • \$\begingroup\$ Interestig @t3chb0t, in fact I had thought of using reflection, but since I only need a subset of the properties made ​​me change my mind, hovewer if I map the model on a view model the problem would cease to exist. Good idea. Thank you. \$\endgroup\$ Commented Jul 7, 2016 at 20:41
  • \$\begingroup\$ Oh, I oversaw this information but in this case you could create an attribute to set on the properties that should be compared like ComparableAttribute and this way you can still do it for some of them but in a more general way and apply it to other types. \$\endgroup\$
    – t3chb0t
    Commented Jul 7, 2016 at 21:14

1 Answer 1

2
\$\begingroup\$

The characteristic numbers are not always indicating bad code...

However, you could do something like that to improve the numbers:

private static List<Difference> RetrieveDifferences(IEnumerable<AddressPoco> newValues, AddressPoco item)
{
    var c = newValues.SingleOrDefault(x => x.xedsAddressID == item.xedsAddressID);
    if (c != null)
        return GetDifferences(c, item).ToList();

    return Enumerable.Empty<Difference>().ToList();
}

private static IEnumerable<Difference> GetDifferences(AddressPoco newAdress, AdressPoco oldAddress)
{
    if (item.xgeoCountryID != c.xgeoCountryID)
        yield return new Difference("Country", item.xgeoCountryID.ToString(), c.xgeoCountryID.ToString()));
    if (item.AddressTypeName != c.AddressTypeName)
        yield return new Difference("Address Type", item.AddressTypeName, c.AddressTypeName));
    if (item.City != c.City)
        yield return result.Add(new Difference("City", item.City, c.City));
    if (item.POBox != c.POBox)
        yield return result.Add(new Difference("POBox", item.POBox, c.POBox));
    if (item.MainContact != c.MainContact)
        yield return result.Add(new Difference("Main Contact", item.MainContact, c.MainContact));
    if (item.PostCode != c.PostCode)
        yield return result.Add(new Difference("PostCode", item.PostCode, c.PostCode));
    if (item.Street != c.Street)
        yield return result.Add(new Difference("Street", item.Street, c.Street));
}
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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