U
U
Uncle Bogdan2022-02-08 15:19:32
C++ / C#
Uncle Bogdan, 2022-02-08 15:19:32

Do I need to somehow fix such overloads?

Hello! I have a static class with extension methods. And there is a Move method.

First
public static void Move(this Transform transform, Vector3 target, float time)
        {
            if (time < 0) Debug.LogError("Time can't be negative");

            var start = transform.position;
            var deltaTime = 0f;

            var thread = _updateThreads.CreateThread();

            Observable.EveryUpdate().Subscribe(action =>
            {
                if (deltaTime >= 1)
                {
                    transform.position = target;

                    _updateThreads.ClearThread(thread);
                }

                transform.position = Vector3.Lerp(start, target, deltaTime);

                deltaTime += Time.deltaTime / time;

            }).AddTo(thread);
        }


So far everything is fine, but then I needed more movement with the help of AnimationCurve and then I created an overload.

Second
public static void Move(this Transform transform, Vector3 target, float time, AnimationCurve curve)
        {
            if (time < 0) Debug.LogError("Time can't be negative");
            if (curve == null) Debug.LogError("Curve is null");

            var start = transform.position;
            var deltaTime = 0f;

            var thread = _updateThreads.CreateThread();

            Observable.EveryUpdate().Subscribe(action =>
            {
                if (deltaTime >= 1)
                {
                    transform.position = target;

                    _updateThreads.ClearThread(thread);
                }

                transform.position = Vector3.Lerp(start, target, curve.Evaluate(deltaTime));

                deltaTime += Time.deltaTime / time;

            }).AddTo(thread);
        }


If you look, almost nothing is different, only a new parameter, a check, and instead of deltaTime it says curve.Evaluate(delta)

Do you need to get rid of this and somehow try to make it shorter?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
@
@insighter, 2022-02-08
@motkot

The fact that you introduce new methods is normal. But inside they can use each other. You have violated the DRY principle.
Rewrite it so that the first Move calls the overloaded variant with some default parameter.
Yes, there seems to be such a thing as default parameter values, but now in .NET the general trend is to abandon them in the direction of adding method overloads.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question