I have many math types like Point3, and I am running into the dilemma of implementing operators, instance and static methods for arithmetic.
So say the type is Point3. Where a, b, c is a Point3, I sure wanna be able to say:
c = a + b;
But should I also implement:
c = Point3.Add (a, b);
And this:
c = a.Add (b);
To me #3 is useless and less readable than #1. And #2 seems like pointless unless you have an interface for Add, Subtract, Multiply, Divide, etc.
What do you recommend? Is there any problem or drawback with just having the operators (+, -, *, /)? Would this impede the generics arithmetic (I know it doesn’t support it directly, but maybe having static methods would be useful in a workaround)?
Would a guidelines for this matter whether it’s a class or a struct?
EDIT: Also for #3, I forgot to mention that this is for an immutable type, so returns a new Point3, instead of changing a.
In general, I think Microsoft seems to have kind of done A+B in most of their classes similar to this.
For samples, see the entire System.Windows.Media.Media3D namespace, as well as the XNA math classes. Both have point/vector/quaternion/matrices/etc, and use Class.Operator(a,b), and occasionally do c = a + b;
Personally, I would do the first where it makes sense and is clear, and always do the second option. I usually implement the first using the second option (the operator is implemented using the static method).