I'm currently working on a .NET Core 3.1 application.
I need to refactor a code smell to reduce method complexity - in my method there are lots of repetitive elements.
All properties in my class are of type string. I need to check if the passed reference holds the same value as the property. If a referenced value differes, I need to set the flag to true and correct the Property value with the referenced one.
public string Name { get; set; }
// ... all Properties are of type String!
public bool MyMethod(Apple myApple)
{
var flag = false;
myApple.Id = Id;
if (Name != myApple.Name)
{
Name = myApple.Name;
flag = true;
}
if (Description != myApple.Description)
{
Description = myApple.Description;
flag = true;
}
if (ShortDescription != myApple.ShortDescription)
{
ShortDescription = myApple.ShortDescription;
flag = true;
}
if (Location != myApple.Location)
{
Location = myApple.Location;
flag = true;
}
if (KindId != myApple.KindId)
{
KindId = myApple.KindId;
flag = true;
}
if (Expiration != myApple.Expiration)
{
Expiration = myApple.Expiration;
flag = true;
}
if (PurchaseDate != myApple.PurchaseDate)
{
PurchaseDate = myApple.PurchaseDate;
flag = true;
}
return flag;
}
Do you know how to reduce complexity in my method and perhaps get rid of the repetetive elements?