I always heard the rule that changing a parameter that was given to the function by value is bad coding style. Instead it is preferred to create a copy that is modified.
But I think in the following case it would be acceptable to change the function parameters. What do you think is the best way to do it?
Point3f GetWorldPoint(int x, int y)
{
x = saturate(x, 0, width);
y = saturate(y, 0, height);
...
}
template<typename T>
T saturate(T val, T min, T max) {
return std::min(std::max(val, min), max);
}
I'm going to stick my neck out here and say it's fine. For example:
template<class T>
Matrix4x4<T> Transpose(Matrix4x4<T> m)
{
return m.Transpose();
}
or
template<class T>
Matrix4x4<T> Transpose(Matrix4x4<T> const & m)
{
Matrix4x4<T> n(m);
return n.Transpose();
}
Which is more concise?
I have some objection with using
Point3f GetWorldPoint(int x, int y)
{
x = saturate(x, 0, width);
y = saturate(y, 0, height);
...
}
Because even semantically, x and y aren't the same before and after the saturate function (they are saturated after all). In more complex situations, this might become confusing. Of course, in obvious functions, it's still straightforward that after saturate, you are now using the saturated x.
Anyway, there is nothing wrong with
Point3f GetWorldPoint(int origX, int origY)
{
int x = saturate(origX, 0, width);
int y = saturate(origY, 0, height);
...
}
It is clearer and an optimizing compiler will create similar code in both cases.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With