Is it not bad practice to have 3 functions with the same name?

I feel like anyone who reads this code is going to be so confused, as I am, when trying to understand why we have 3 separate functions, all with the same name, but with different overloads. I feel like it would be so much cleaner and easier to read if the function names were more descriptive to actually explain what’s going on. How often is this name repetition done in the professional field?

Overloads exist and are used quite often. This is usually because they do the same thing so the name reflect what they do regardless of the parameters. You will find that C# and Unity’s own methods do this, as do many, many other professional APIs.
Take for example Vector3.SmoothDamp(...). It has 3 overloads. Quaternion.LookRotation(...) has 2. There are many more you will find. They all do the same thing, but the overloads allow the method to take certain bits of information that you may have at your disposal for ease of use. I may have a method that moves my character to a position. It would make sense to call it MoveToPosition because that’s what it will do, and I could have overloads to take in the x, y and z coordinates, or a Vector3.

public void MoveToPosition(float x, float y, float z)
    MoveToPosition(new Vector3(x, y, z));
public void MoveToPosition(Vector3 position)
    // Move the character to the position

This is not a great example because I could have created the Vector3 myself, but the point is that I would be hard pressed to come up with a name that explains what each does if they both do the same thing. And this also saves me from having to create the vector myself.


I see. Thank you so much for your in-depth response!

Privacy & Terms