- Home /
Many methods taking Vector3 as argument, all have overloads for taking 3 floats instead. Is there a better approach?
My code has a lot of methods that take a Vector3 as an argument, something like this:
public void Method(Vector3 vector)
{
// do stuff
}
and many of them have overloads like this:
public void Method(float x, float y, float z)
{
Method(new Vector3(x, y, z));
}
These methods are called all over the place, and it keeps the code a teensy bit cleaner because some places work with floats, some with Vectors, and I don't have to worry about converting one before using these methods.
Is there a better way to achieve this? Some kind of decorator pattern that automatically generates the overloads? Or is this just an overall bad idea, and I should stick with only one version of the parameter list?
That's how Unity does it: transform.Translate take a Vector3, or 3 floats. Another common trick is having z default to 0: (x,y,z=0).
Answer by JojoIV · Jan 04, 2019 at 12:37 AM
In my experience the way described by you is definitely appropriate and hasn't a big impact on performance. It might be a bit messy some times so I would usually stick to Vector3 instead and dont use overloads at all because the Vector3 class has many helpfull methods and it is always easy to call a method like this without overloads: Method(new Vector3(x, y, z));