Skip to content

Move comparisons for equality of value types before equality comparisons of collections #1090

Description

@RalfKoban

We should report that equality comparisons within logical AND conditions should be done by comparing values first and collections last.

Example:

public class SomeData
{
    public int Id { get; set; }
    public List<Data> Data { get; set; }

    public bool Equals(SomeData other)
    {
        return ((Data == other.Data) || (Data != null && Data.SequenceEqual(otherData))) && Id == other.Id;
    }
}

should be reported and changed into:

public class SomeData
{
    public int Id { get; set; }
    public List<Data> Data { get; set; }

    public bool Equals(SomeData other)
    {
        return Id == other.Id && ((Data == other.Data) || (Data != null && Data.SequenceEqual(otherData)));
    }
}

The reason is that comparison on value types is normally faster than comparing lists.

Note: The term "Value types" here include enums, Guids and strings.

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status
    In progress

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions