Skip to content

UNT0034 and UNT0035 need more explaining? #409

@MightyCreak

Description

@MightyCreak

Hi!

I wonder about the relevancy of the UNT0034 and UNT0035 as to why they are in the "Performance" category.

As far as I can understand, in the example given in UNT0034, it's advised to go from there:

        Vector3 v3 = ...;
        Vector2 v2 = ...;
        var distance = Vector2.Distance(v2, new Vector2(v3.x, v3.y));

to there:

        Vector3 v3 = ...;
        Vector2 v2 = ...;
        var distance = Vector2.Distance(v2, v3);

But, if I'm not mistaken, the only thing it does is calling the implicit cast here: https://github.com/Unity-Technologies/UnityCsReference/blob/master/Runtime/Export/Math/Vector2.cs#L387

public struct Vector2 : IEquatable<Vector2>, IFormattable
{
    public static implicit operator Vector2(Vector3 v)
    {
        return new Vector2(v.x, v.y);
    }
}

And, to me, it feels like the result is exactly the same: instead of doing a new explicitly, it does a new implicitly.

And, as a result, I think that mixing Vector2 and Vector3 makes the code less readable and potentially less maintainable. (it's obvious in this example where variables are named v2 and v3, but it's a bit different when you have a bunch of variables in your algorithm).

So my question is: am I missing something that would explain why there is a performance improvement? (if I am, could you explain it to me? I'm really interested to understand why); or should these diagnostics be re-categorized? to "Correctness", maybe?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions