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?