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

Finding the difference between postal addresses

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

Problem

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 RetrieveDifferences(IEnumerable newValues, AddressPoco item){
  var result = new List();
  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?

Solution

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

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

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

    return Enumerable.Empty().ToList();
}

private static IEnumerable 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));
}

Code Snippets

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));
}

Context

StackExchange Code Review Q#134025, answer score: 2

Revisions (0)

No revisions yet.