I'm unsure of how to Name and clearly distinguish between methods that deals with comparisons.
I know of the following two established concepts:
.Equals:
A method to see if two objects refer to the same instance
.Compare:
A method often used when determining sort order among objects
But what to name a method when you are interested to see if two different object instances refer to the same "logical" object, or when comparing values of two different object instances? Let me give you an example with a class named Photo
.
public class Photo : IEquatable<Photo>
{
public string FileName { get; private set; }
public string InFolder { get; private set; }
public string FullFilePath { get; private set; }
public string FileExtension { get; private set; }
public long FileSize { get; private set; }
public DateTime FileLastModified { get; private set; }
public DateTime FileCreated { get; private set; }
public ExifData EXIF
{
get
{
if (this._exif == null)
LoadExtendedProperties();
return this._exif;
}
set { this._exif = value; }
}
public Photo(string fullFilePath)
{
//...
}
public override int GetHashCode()
{
return this.FullFilePath.GetHashCode();
}
private void LoadExtendedProperties()
{
//...
}
public bool Equals(Photo other)
{
if (object.ReferenceEquals(this, null) || object.ReferenceEquals(other, null))
return false;
if (object.ReferenceEquals(this, other))
return true;
else
return false;
}
/// <summary>
/// Do they refer to the same photo (file)
/// </summary>
public bool UnnamedCompareMethodA(Photo other)
{
if(object.ReferenceEquals(other, null))
return false;
if (this.FullFilePath == other.FullFilePath)
return true;
else
return false;
}
/// <summary>
/// Do all properties have the same value?
/// Usefull when determining if a Photo has been modified since last loaded
/// </summary>
public bool UnnamedCompareMethodB(Photo other)
{
// Note: Instead of manually adding fields; Reflection might be a better choice.
// Though that might bring other challenges as some fields should be left out when comparing.
// If any object is null then they are NOT considered equal
if (object.ReferenceEquals(this, null) || object.ReferenceEquals(other, null))
return false;
if (this.FileLastModified != other.FileLastModified)
return false;
if (this.FullFilePath != other.FullFilePath)
return false;
if (this.FileSize != other.FileSize)
return false;
if (this.EXIF.Title != other.EXIF.Title)
return false;
if (this.EXIF.Rating != other.EXIF.Rating)
return false;
if (this.EXIF.PictureTaken != this.EXIF.PictureTaken)
return false;
if (!this.EXIF.Orientation.Equals(other.EXIF.Orientation))
return false;
if (!this.EXIF.Dimensions.Equals(other.EXIF.Dimensions))
return false;
if (!this.EXIF.Location.Equals(other.EXIF.Location))
return false;
return true;
}
}
Question:
What to name UnnamedCompareMethodA()
and UnnamedCompareMethodB()
? I would like to have "Equal" somewhere in the name but I feel like that would create confusion as they are not related at all with .Equals()
.
LoadExtendedProperties
implementation. CR answers typically touch on every aspect of the code. I presume your ctor only calls the private setters, and thatLoadExtendedProperties
can never throw an exception? \$\endgroup\$